Skip to main content

Selfmade Vagrant box

· 2 min read

I couldn't find a good box with a GUI on Vagrant Cloud.

Build process​

info

I'll use Docker as an analogy throughout.

config file​

We describe the Vagrant box (think Docker image) in an HCL configuration, much like Terraform.

source "vagrant" "ubuntu" {
add_force = true
communicator = "ssh"
provider = "virtualbox"
source_path = "ubuntu/focal64"
}

build {
name = "learn-packer"
sources = [
"source.vagrant.ubuntu"
]
}
tip

Useful commands:

packer validate packer.hcl # Validate the file
packer fmt . # Format the files and, I believe, validate them too

build​

Build the image with Packer, which plays a role similar to the Docker engine here.

packer build packer.hcl

After a while, a .box file will appear in the learn-packer directory. Think of it as the Docker image.

push​

Upload the file to Vagrant Cloud. You'll need its checksum, which you can calculate with md5, for example.

provisioner​

note

Ansible is my preferred provisioner, though shell works too.

source "vagrant" "ubuntu" {
add_force = true
communicator = "ssh"
provider = "virtualbox"
source_path = "ubuntu/focal64"
}

build {
name = "learn-packer"
sources = [
"source.vagrant.ubuntu"
]
provisioner "ansible" {
playbook_file = "./ansible/packer-playbook.yml"
}
}

post-processors​

caution

I haven't tested this.

These steps run after the build, for example publishing to Vagrant Cloud, much like docker push.

GitHub repo

Comments and reactions