Packer Definitions
Packer concepts explained
Website Visitors:If you’re looking for Packer definitions, then you are looking at the correct article. In this article, we will discuss some basic Packer components and definitions.
Let’s jump right in.
Packer Components
Packer templates consist of 3 components:
-
Builders: [Builders](Builders | Packer by HashiCorp) are used to create machine images as part of image creation process. Builders connect to your cloud/on-prem environment using the access keys, secret keys etc…, connect to your infrastructure and create the template. Packer Builders are available for almost all platforms. You can view the list of all builder plugins [HERE](Packer plugins | Packer by HashiCorp).
-
Provisioners: Provisioners are used to install and configure softwares or run custom scripts or commands on the golden VM/base VM before an image is created. After provisioners execution is completed, builders create the image. Example provisioners are chef, puppet.
-
Post-Processors: Post-processors runs any tasks after the image creation is complete. This include uploading artifacts, sharing the image id to other AWS accounts etc…
Other definitions
-
Artifacts: Artifacts are the results/outputs of a single build that represent IDs/files representing a machine image. An example of artifact could be the AMI ID of the image created in AWS. For VMware this will be the folder containing all the template files created by Packer.
-
Builds: Builds are tasks that are carried out when Packer creates a image. Multiple builds run in parallel.
-
Data Sources: Data sources fetch data from outside Packer and make it available to use within the template. Example of data sources include Amazon AMI, and Amazon Secrets Manager.
-
Templates: Templates are either HCL or JSON files which define one or more builds by configuring the various components of Packer. Packer is able to read a template and use that information to create multiple machine images in parallel.
Packer command parameters
-
Build: Using this build command packer starts the image creation process and finally generate set of artifacts. All the builds specified within a template are executed in parallel. Artifacts are outputted at end of the build. This command takes a template file and runs all the builds within it and generates set of artifacts. Ex:
Packer build file.json
-
Console: you can access the variables declared in packer config file using the console. Ex:
Packer console
-
fix: Fix parameter will identify incompatible blocks of code in a template file and changes it, to make the code compatible with latest versions of Packer. By default, it outputs to the console directly. You have to redirect the output to a file if you want to save it.
-
fmt: Packer will format your json, hcl or hcl2 files to canonical format. If you have added extra indentation in your packer script file, aka template file, you can format your script indentation using the fmt parameter. Packer will read all .json or .hcl files in the current folder and format the code within those json or hcl files.
-
init: Downloads required packer plugins. It is recommended to run this command multiple times.
-
inspect: Lists all the builders, provisioners, variables etc.. by reading your script. You don’t have to go through your script to analyze the values used in the script. Inspect parameter will do it for you. Ex:
Packer inspect file.json
-
validate: Validate parameter will validate the syntax and configuration of a template file. Ex:
Packer validate file.json
-
hcl2_upgrade: Upgrades your json template file to hcl2 configuration syntax. When you run the command, it creates a new file with extension .pkr.hcl Ex:
Packer hcl2_upgrade file.json
Debugging Information
You need to use the Packer environment variable PACKER_LOG and assign it any value other than empty value and run the build command as shown below. This will show debugging information on the screen. You can add any value to PACKER_LOG variable.
PACKER_LOG=10 packer build templatefile.json
You can also add a -debug
parameter in the command to prompt you to hit enter at every step Packer has in the image creation process. If you cancel it, Packer stops the script.
PACKER_LOG=10 packer build -debug templatefile.json
Suggested Article
Now that you know about Packer definitions, continue reading our other article Packer Variables here. All other DevOps categories are listed here: DevOps Tools. Have a look at the posts as per your topic of interest.
Conclusion
In this tutorial, we’ve explained Packer definitions, commands and debugging the commands. We hope you have learned something new in this article.
Please feel free to share your thoughts about this article in the comments section below.
Your inbox needs more DevOps articles.
Subscribe to get our latest content by email.