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.
- packer.hcl
- packer-playbook.yml
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"
}
}
---
- name: install docker engine ubuntu
hosts: all
become: true
become_method: sudo
tasks:
- name: 1. Update the apt package index and install packages to allow apt to use a repository over HTTPS
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
- python3-pip
- awscli
state: latest
update_cache: true
- name: 2. Add Docker’s official GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: 3. Add Docker Repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu focal stable # FIXME move to var
state: present
- name: 4. Install docker engine
apt:
name: docker-ce
state: latest
update_cache: true
# Docker post-install steps for ubuntu
- name: 1. Make sure that group "docker" exists
group:
name: docker
state: present
- name: 2. Add aws user to docker docker group
user:
name: ubuntu
groups: docker
append: true
post-processors
caution
I haven't tested this.
These steps run after the build, for example publishing to Vagrant Cloud, much like docker push.
