Getting Started with Packer
This article was written as a short primer on Packer and a quick introduction to how it works.
“Give me a place to stand and with a lever I will move the whole world. ” Archimedes
- Introduction
- Helpful Terms
- The Packer Process
- Example Template
- Closing thoughts
- Links
- Acknowledgements
Introduction
This article was written as a short primer on Packer and a quick introduction to how it works. For more specific detail, please check the documentation listed here: Packer Documentation
Packer is a tool that lets you build multiple machine images from a single template. It’s the tool I’ve found best used for creating golden images.
Packer uses either HCL or JSON-based templates as input data for the image build. Packer uses the template as an instruction manual to communicate with the provider, build the specified image, and run additional provisioning steps.
From version 1.7.0 and on, HCL2 is the de-facto standard and preferred way to build packer configurations.
Packer Templates
Helpful Terms
Some helpful terms to keep in mind when working with Packer:
Templates
JSON or HCL2 coded files that detail how your build will execute, very much like a cooking recipe. A bakingrecipe might say to bake something at 400F for 20 minutes, and a template will tell Packer to use the vSphere builder to create a VM with 2GB of RAM.
Artifacts
An Artifact is specific data left over from a builder during a run, and It can be unique to the builder used. For instance, the vSphere builder artifact is a directory of files detailing the VM built, while an EC2 builder artifact is a resultant set of AMI IDs.
Builds
The overall packer execution run. “The packer build creates an Ubuntu 20 vSphere VM."
Builders
Think of these as modules or plugins tasked with building the specific image on the platform, i.e., the execution engine that communicates with vSphere and creates the VM.
Commands
These are simply the commands you can run against the packer executable, i.e.: packer build
,
which executes a build command.
Data sources
These components allow Packer to fetch specific data from a provider and use it in a build, i.e., getting an AMI ID from amazon during the build process by telling Packer you want the latest ubuntu x86 image.
Provisioners
Provisioners work similar to builders, except they interact with the machine during run-time before converting to a static image. For example, an Ansible provisioner would configure and run ansible-related tasks.
Post Processors
After the provisioner and builders have finished, Post-processors can complete operations against the build artifacts. For example, the Manifest Post-processor writes a JSON file with a list of the artifacts created during a run.
The Packer Process
How it all comes together when you run packer build
Example Template
Below are several code examples of sections in a packer template. The full template is available at the following link: Kalen Arndt - packer-vsphere-cloud-init template
Header
In this block, we define the required packer version and the builder plugins and versions.
|
|
Locals
In this section, we define local variables, and in this example, we are using a build time variable used for logging.
|
|
Source
This section defines everything that the builder will need when communicating and instructing the provider to create the machine.
This section can be pretty large, so I’ve only included a small example selection.
|
|
Build
In this block, we identify the builder, along with the provisioner and post-processor configurations.
|
|
Closing thoughts
This article has been a quick primer on Packer, the process, and fundamentals. If you want to learn more, I suggest looking at Hashicorp’s Packer tutorials and documentation.
The example HCL template shown is part of ubuntu on vSphere packer build, which I will cover in future posts.
Thanks for reading!