Packer: build multiple images easily

| 0 comments

0.00 avg. rating (0% score) - 0 votes

packer

Packer is one of the tools I’ve used in the past to build VirtualBox boxes. You can find what I’ve done on my GitHub account.

For Smash project, I wanted to make a packer configuration to manage Docker and VirtualBox. I also wanted to call Ansible to build specific images for each needs. The goal is to be able to build cloud image ready to start, without any special dependencies. This because I need different usages:

  • Production: multiple images for the cloud (VM/Container)
  • CI: multiple images using Docker
  • Dev: multiple images using Docker or VirtualBox

I did not started to manage the cloud provider for the moment as I’m not sure which one I’ll be using. Anyway it won’t be complicated as the biggest part has been done here. First of all, I declared the default vars in the Packer file:

Here I specified the Debian version which will be automatically downloaded by Packer (for VirtualBox). Then the Ansible inventory file which is containing the group of hosts where the generated image will belong to. Example:

Then we are declaring the 2 builders. The first one for VirtualBox and the second for Docker:

We’re calling here the desired version of Debian image in the variables. The automated installation of VirtualBox is ensured by Debian preseed. VirtualBox will also pass required args to Debian grub boot for preseed. Regarding Docker, it’s simpler as it will automatically pull from the official Docker repository the required distribution.

Then you need to use post processor to request the output format from Packer. Here for VirtualBox, I require an export in a .box format with a maximum compression level. And for Docker, I add the image locally to my  list of images:

Regarding the provisioner part, the way to call VirtualBox scripts and Docker are different. VirtualBox uses sudo command while Docker doesn’t need it. As you can see, I’m executing several scripts (some are the same and others are specific):

Ansible is also executed to create a complete image (and specific vars can be added). Of course this is a little bit boring to create a new image on every release (but can be automated), however when your goal is to auto-scale your images in a cloud environment, it’s relevant to be not dependent of tier party applications (GitHub, Apt/Yum repo etc…). That’s why this solution is better in my opinion.

If you’re wondering what the scripts are doing:

  • “scripts/update.sh”: install curl

  • “scripts/sshd.sh”: install SSH and set 2 options

  • “scripts/networking.sh”: reset network device interface name

  • “scripts/ansible_prequesites.sh”: install prerequisite for any Ansible client

  • “scripts/sudoers.sh”: install sudo and give to vagrant user all privileges

  • “scripts/vagrant.sh”: create vagrant user and add vagrant public key to vagrant user (for VirtualBox) and to root user (for Docker)

  • “scripts/ansible_bin.sh”: install Ansible to be able to run playbooks locally

Now we’re ready to play ! You can have a variables file that will override the default set vars in your Packer file. Example:

Now if I want to create an exported VirtualBox .box with default vars, I only have to run:

And if I want a Docker container added to your images with my custom vars:

If you do not specify an output format, it will build both in parallel 🙂

Here is the complete packerfile:

Thanks again to Hashicorp and hope you enjoyed explanations !

Author: Deimos

I'm a passionate DevOps. I love transmit my skills and I love working on high availability infrastructures/technologies.

Laisser une réponse