Creating vSphere VM templates with Packer (part 4) - vSphere variables

In part 4 of this series it's finally time to introduce some JSON. The variables that I re-use in every template for telling Packer where to provision machines.

Creating vSphere VM templates with Packer (part 4) - vSphere variables

Now that variables and templates etc have been covered, let's not waste any more time and dig in!

As I wanted to build templates for multiple operating systems (both Windows and Linux) I was always going to need multiple template files. The provisioners needed for Windows would not work with Linux and vice-versa. Script for Ubuntu, CentOS and Photon might not be portable between the different OSs. And it would be better if I could build one OS in isolation from the others for when something changes.

Hence I needed a template file (JSON) for each OS and major version, meaning 5 template files needed.

  • centos7-vsphere.json
  • centos8-vsphere.json
  • photon3-vsphere.json
  • windows2016-vsphere.json
  • windows2019-vsphere.json

As they're all going to be targeting the same vSphere infrastructure, you can imagine that there is some duplicated configuration. Rather than define it 5 times, I opted to create a variable file for the common vSphere settings that I would need. And here it is:

  "var_vsphere_vcenter": "",
  "var_vsphere_username": "administrator@vsphere.local",
  "var_vsphere_password": "password",
  "var_vsphere_datacenter": "uk01",
  "var_vsphere_cluster": "uk01-cls01",
  "var_vsphere_datastore": "uk-p-syn-0001_ds01",
  "var_vsphere_network": "dvpg_172.28.0.0_24",
  "var_vsphere_folder": "/Templates",
  "var_vsphere_iso_datastore": "[V12N-QNP-0001-ISO]"
The contents of var-vsphere.json for my environment

The names of the variables don't matter. I have chosen them to make it clear that they're variables and that they relate to vSphere. In case it's not clear, the images below indicate where the value has come from.

vSphere variable source values (1)

var_vsphere_vcenter is the FQDN of the vCenter host that Packer will connect to.

var_vsphere_datacenter is the name of the datacenter object in vSphere where machines will be built.

var_vsphere_folder is the folder path where Packer will build machines.

vSphere variable source values (2)

var_vsphere_cluster is the name of the vSphere cluster in which I want Packer to build machines.

vSphere variable source values (3)

var_vsphere_datastore is the datastore where machines will be built.

var_vsphere_iso_datastore is the NFS datastore that houses the ISO files that I want to use for builds.

vSphere variable source values (4)

var_vsphere_network is the portgroup name that machines will connect to, it has a small DHCP range on it and can access the internet.

Not shown (for obvious reasons) are var_vsphere_username and var_vsphere_password. This account must have sufficient permissions to create VMs, mount ISO files, change hardware, mark VMs as templates etc. Best practice would be to use an account other than administrator@vsphere.local!

Including these variable in my Packer executions is now simply a case of including the file in my requests, e.g.:

packer build -var-file=var-vsphere.json template.json

The variables can then be used in the builder definitions to instruct Packer about where and how to build my machines. More on that in parts 5 and 6.

  1. Introduction
  2. Configuring Packer server and required files
  3. Variables, builders and provisioners
  4. vSphere variables <-- $this
  5. Windows Server templates
  6. CentOS templates
  7. Further plans (including scheduling packer)

My public Packer repository:

Packer templates used in my v12n cloud. Contribute to mpoore/packer development by creating an account on GitHub.