Variables
In the previous tutorial, you implemented your first provisioner. However, your Packer template is static. Currently if you want to change the contents of the file, you need to manually update your template.
You can use input variables to serve as parameters for a Packer build, allowing aspects of the build to be customized without altering Packer template. In addition, Packer variables are useful when you want to reference a specific value throughout your template.
In this tutorial, you will add Packer variables to parameterize your Packer build, making it more robust.
Prerequisites
This tutorial assumes that you are continuing from the previous tutorials. If not, follow the steps below before continuing.
Install Packer
Create a directory named
packer_tutorial
and paste the following configuration into a file nameddocker-ubuntu.pkr.hcl
.Initialize the Packer template.
Once you have successfully initialized the template, you can continue with the rest of this tutorial.
Add variable to template
Add the following variable block to your docker-ubuntu.pkr.hcl
file.
Variable blocks declare the variable name (docker_image
), the data type (string
), and the default value (ubuntu:jammy
). While the variable type and default values are optional, we recommend you define these attributes when creating new variables.
Treat Packer variables as constants — you cannot update them during run time.
In your Packer template, update your source block to reference the docker_image
variable. Notice how the template references the variable as var.docker_image
.
Tip
The below snippet is formatted as a diff to give you context about
which parts of your configuration you need to change. Replace the content
displayed in red with the content displayed in green, leaving out the leading
+
and -
signs.
Update the second provisioner to print the docker_image
variable. Notice that even though the docker_image
variable was used directly in the source block above, to embed a variable in another string you need to template it using the syntax ${}
, as shown when echoing docker_image
in the provisioner block.
Build image
Build the image.
The output is the same as before, but now the docker_image
variable is being used to specify that the "ubuntu:jammy"
base image should be used.
You'll know it worked if you see the Builds finished. The artifacts of successful builds...
message near the bottom.
Build image with variables
Since docker_image
is parameterized, you can define your variable before building the image. There are multiple ways to assign variables. The order of ascending precedence is: variable defaults, environment variables, variable file(s), command-line flag. In this section, you will define variables using variable files and command-line flags.
Build image with variable file
Create a file named example.pkrvars.hcl
.
Add the following snippet into it.
Build the image with the --var-file
flag.
Notice how the build step runs the ubuntu:lunar
Docker image, the value for docker_image
defined by the variable file.
Note
Never commit sensitive values into source control.
Packer will automatically load any variable file that matches the name *.auto.pkrvars.hcl
, without the need to pass the file via the command line.
Rename your variable file so Packer automatically loads it.
Build the image and notice the Docker image running ubuntu:lunar
. The packer build .
command loads all the contents in the current directory.
Build image with command line flag
Build the image, setting the variable directly from the command line.
Notice how the build step runs the ubuntu:focal
Docker image, the value for docker_image
defined by the command-line flag. The command-line flag has the highest precedence and will override values defined by other methods.
Next steps
In this tutorial, you made your Packer template more robust by parameterizing it with variables. Then, you defined your variables using variable files and command-line flags during the build step, learning variable definition order of precedence in the process. Continue to the next tutorial to create multiple images in parallel using the same template.
Refer to the following resources for additional details on the concepts covered in this tutorial:
- Read more about the Packer variables.
- Learn more about how to use Packer variable blocks.