Reduce Your Virtual Machine Build Time Using Hashicorp Packer: [Part 2 - HCL template components]

automation packer Dec 20, 2023
hashicorp packer

Introduction:


Part 2 covers the concepts of the build process and HashiCorp Configuration Language (HCL) templates.  Builders create machines and generate images from those machines for various platforms.  Multiple builders create the golden image in the Automated Sandbox Framework (ASF).  Let's unpack each of the concepts before we write our HCL template. 

Build Process and Templates:


Packer builds machine images through steps defined in a template written in HashiCorp Configuration Language (HCL). The template specifies the source (e.g., ISO file, cloud instance), builders (e.g., VirtualBox, Amazon EC2), provisioners (e.g., shell scripts, software installers), and post-processors (e.g., Vagrant packaging). Understanding these components is crucial for writing effective templates.

Sources:


Sources define the starting point for your build process. They can be virtual machine images, ISO files, cloud instances, or other pre-configured environments. The chosen source determines the initial state upon which Packer will build your desired image.  We utilize the ISO file source in the ASF.  However, what you learn in this series can be built upon, and you can introduce additional sources into your build.

Builders:


Builders define the platform and tools used to create the machine image. Popular builders include VirtualBox, VMware, Amazon EC2, and Docker. Each builder has specific requirements and capabilities, so choosing the right one is essential.

Packer has the following types of builders. 

  • Plugin: Each plugin has its own associated set of builders.  There are separate builders for EC2, VMware, Virtualbox, etc.  We'll use the VMware and Vagrant plugins as part of this series.
  • File: The file builder creates an artifact from a file.
  • Null: The null builder sets up an SSH connection and runs the provisioners.
  • Custom: You can write new builders.
  • Community-Supported: The Packer community develops and maintains builders for several additional platforms.

Next, we'll look at Provisioners. 

Provisioners:


Provisioners are the heart of customization. They execute scripts or software installations to configure the built environment according to your requirements. This could involve installing software, setting up user accounts, configuring network settings, and more.  Below are a few provisioners utilized in this series:

  • Powershell: This provisioner runs Powershell scripts on Windows machines.  It assumes that the communication is WinRM (which we use in the ASF).
  • File: The file provisioner uploads files to machines built by Packer.  We use this provisioner to copy an autounattend.xml file, PowerShell script, and SetupComplete.cmd file from the host to the guest.  
  • Windows-restart: Initiates a reboot on a Windows machine and waits for the machine to come back online.
  • Shell:  This one will run a shell script on the image.

Post-Processors:


Post-processors handle final tasks after the build process is complete.  They are used to package the output image in various formats, upload it to cloud storage, or create it.  We'll utilize the Vagrant post-process in this series.  Here's where Packer shines: you can make the same image for multiple platforms, which can be on-premises or in the cloud.  It doesn't matter; Packer doesn't care.

Variables:


Variables are reusable values that can be referenced throughout your template. They help you maintain a DRY (Don't Repeat Yourself) approach, making your template more concise and easier to manage. Variables can be used for configuration values, file paths, or other frequently used data. 

HCL (HashiCorp Configuration Language):


Packer templates can be written in JSON or HCL.  We'll use HCL.  This is a simple yet powerful language designed for configuration files.  Understanding the basic syntax and concepts of HCL is crucial for writing valid and effective templates. 

Conclusion


In this post, we covered the essential components of Packer.  In our next post, we'll cover the HCL template in more depth.   If you're anxious to get started with your sandbox, look at the free guide below.

 

See you in the next post, where we'll start creating the HCL template.

Whenever you're ready, there is one way I can help you gain hands-on experience:Ā 

Automated Sandbox Fundamentals: I teach how to build a virtual lab using automation in this course. Learn how toĀ create golden images, using both Windows and Linux, to easily spin up and add additional machines to your sandbox.Ā  It's packed with 8 modules and the scripts you'll need to build your environment.Ā  Start small, andĀ scale as needed by easily changing the configuration file included with the course.

Give Me the Details