Unlocking the Power of Packer

Creating vSphere Templates Using Packer: Part 3 – Variables

Welcome back to part three of my Creating vSphere Templates using Packer series. In this article, we will explore variables and why they are useful in templating code. We will discuss two types of input variables: user-defined variables and environment variables. We will also go over how to declare and define these variables in a variable declaration file and how to use them in your source block.

Why Use Variables?

Variables allow you to specify customizations to your templating code without having to edit your actual build files. This can be useful when reusing code for multiple templates. Instead of hardcoding values, you can define them as variables and reuse them throughout your templates.

Types of Variables

There are two types of input variables we will discuss: user-defined variables and environment variables.

User-Defined Variables

User-defined variables are variables that you define in your configuration files. You declare these variables in a variable declaration file, which we will talk about later. These variables can be any data type, and you can set default values for them.

Environment Variables

Environment variables are especially useful if you want to use Packer as part of a workflow or automation pipeline, or to pass in secrets (passwords or keys) into the workflow from a secret management tool. You still declare all your variables in your variable declaration file, but instead of providing a value, you create environment variables in your terminal session.

How to Declare and Define Variables

To declare and define variables, you will need to create a variable declaration file (pkr.hcl). In this file, you can specify default values for your variables, set types for the values, and add descriptions to help you understand the purpose of each variable.

Here is an example of a basic user-defined variable:

“`hcl

variable “vsphere_datastore” {

type = string

}

variable “vsphere_portgroup_name” {

type = string

}

“`

In this example, we are declaring two variables called ‘vsphere_datastore’ and ‘vsphere_portgroup_name’. We have not set any default values or types for these variables.

Here is an example of a variable with a default value and type:

“`hcl

variable “vsphere_password” {

type = string

default = “mysecretpassword”

}

“`

In this example, we are declaring a variable called ‘vsphere_password’ with a default value of ‘mysecretpassword’. We have also set the type to ‘string’.

Here is an example of a variable with a description:

“`hcl

variable “vsphere_datastore” {

type = string

description = “The name of the vSphere datastore to use.”

}

“`

In this example, we have added a description to the ‘vsphere_datastore’ variable. This helps us understand the purpose of the variable.

How to Use Variables in Your Source Block

To use variables in your source block, you will need to reference them using ‘var.’ followed by the name of the variable. For example:

“`hcl

resource “vsphere_virtual_machine” “example” {

// …

variable.vsphere_datastore = var.vsphere_datastore

variable.vsphere_portgroup_name = var.vsphere_portgroup_name

}

“`

In this example, we are referencing the ‘vsphere_datastore’ and ‘vsphere_portgroup_name’ variables in our source block.

Conclusion

Variables are a powerful tool in Packer that allow you to customize your templates without having to edit your build files. In this article, we have discussed user-defined and environment variables, how to declare and define them in a variable declaration file, and how to use them in your source block. Be sure to check out HashiCorp’s official documentation for even more options and examples. Thanks for reading!

Copyright © The Small Human Cloud 2024

Powered by Hugo