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.
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.
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:
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.
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.
var_vsphere_cluster is the name of the vSphere cluster in which I want Packer to build machines.
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.
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 firstname.lastname@example.org!
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.
Other posts in this series:
- Configuring Packer server and required files
- Variables, builders and provisioners
- vSphere variables <-- $this
- Windows Server templates
- CentOS templates
- Further plans (including scheduling packer)