Taylor Vories

Bloggy stuff

Packer HCL, Windows, and vsphere-iso

2020-07-20 2 min read Taylor Vories

The code

I learn by example, so see my github repo for the full source code!

https://github.com/tvories/packer-vsphere-hcl

Packer, as of 1.5, now supports HCL. This gives it a lot more flexibility over regular JSON and makes it very similar to terraform.

There aren’t a lot of great examples of how to convert packer from json to HCL, though, and practically zero that leverage vsphere-iso as the builder. Most of the HCL example code for packer is written for aws. Windows is especially difficult to find examples for, so I figured I would try to share my configuration.

Directory Structure

boot_config - Stores kickstart, answerfiles, and preseed files.

boot_config/
├── 2019
│   └── Autounattend.xml
├── centos-8
│   └── centos8-ks.cfg
├── ubuntu-18.04
│   └── preseed.cfg
└── ubuntu-20.04
    ├── meta-data
    ├── preseed.cfg
    └── user-data

These are named as to be targeted by the packer variables os_family and os_version. This is so you can dynamically pick the correct boot_config files.

drivers - VMware Tools drivers for Windows pvscsi driver and VMXNET3 driver. This allows Windows to come up with storage and network without needing to have VMware Tools installed.

scripts - Scripts used for bootstrapping the OSes.

root directory - Where the actual packer files exist.

Key files

vsphere.pkr.hcl

This is where the sources and builds are defined. Every build is triggered from this file. It includes Ubuntu, Centos, and Windows sources and builds.

vsphere.auto.pkrvars.hcl

These are variables that are loaded automatically (anything with a .auto.pkrvars.hcl will load automatically in the same directory) and are the same for every build. This is where the vsphere variables are populated.

variables.pkr.hcl

This is where the hcl variables are declared. You could potentially put this at the top of your vsphere.pkr.hcl file, but it’s good practice to keep your variables files separate.

There are some defaults defined in this file, but for the most part variables are declared in the individual OS variable files.

Individual OS variable files

These files are specifically provided during the packer command to tell packer which build to process. An example packer command would be: packer build -force --only vsphere-iso.windows --var-file=2019.pkrvars.hcl .

Note the trailing period in that command. That is telling packer to build everything in the directory, which is required to get the auto.pkrvars.hcl to work correctly. Note in that command the --only flag. That tells packer to only build the vsphere-iso.windows source.

comments powered by Disqus