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 namedaws-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
Packer has two types of variables: input variables and local variables. In this section, you will use both input and local variables to generate a unique AMI name. This way, you don't need to update your AMI name before each new run.
Add input variable
Add the following variable block to your aws-ubuntu.pkr.hcl
file.
Variable blocks declare the variable name (ami_prefix
), the data type (string
), and the default value (learn-packer-linux-aws-redis
). While the variable type and default values are optional, we recommend you define these attributes when creating new variables.
You can override the values of input variables at run time using command line flags, environment variables, or special variable definitions files. However, nothing can change the value of an input variable once you run Packer. See Assigning values to input variables for more information.
Add local variable
Add the following local variable block to your aws-ubuntu.pkr.hcl
file.
Local blocks declare the local variable name (timestamp
) and the value (regex_replace(timestamp(), "[- TZ:]", "")
). You can set the local value to anything, including other input variables and locals. Locals are useful when you need to format commonly used values.
In this example, Packer sets the timestamp
local variable to a formatted timestamp using functions.
Unlike input variables, you cannot override the value of local variables. Local variables are set to expressions that are evaluated at run time. The expressions can reference input variables, local variables, data sources, and HCL functions.
Update AMI name
In your Packer template, update your source block to reference the ami_prefix
variable. Notice how the template references the variable as var.ami_prefix
.
Build image
Build the image.
Notice how the Packer creates an AMI where its name consists of learn-packer-linux-aws-redis
, the default value for the ami_prefix
variable, and a timestamp.
Build image with variables
Since ami_prefix
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
and add the following snippet into it.
Build the image with the --var-file
flag.
Notice how the AMI name starts with learn-packer-aws-redis-var-
, the value for ami_prefix
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 how the AMI name starts with learn-packer-aws-redis-var-
. 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 AMI name starts with learn-packer-aws-redis-var-flag
, the value for ami_prefix
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 input and local 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.
- Learn more about how to use Packer local variable blocks.
- Read more about Packer functions.