Creating vSphere VM templates with Packer (part 1) - Introduction
Bored of hand-rolling vSphere templates? I was! This is part 1 of a series that documents creating vSphere templates using Packer.
HashiCorp Packer is one of those things I've been meaning to get to grips with for some time. Other things always seemed more important so it got pushed to the bottom of my task list.
Lately however, I've been deploying more linux servers (CentOS) in my homelab and I was aware that my templates very quickly got out of date in terms of patches etc. I'm very much of the opinion that once something is a template, you shouldn't ever power it back on again unless you're cloning it to be a new VM. Ok so it's only me using my homelab so my templates are always going to be in a known state (known to me that is), but I've still never liked turning them on, patching them, trying to do a tidy-up and then converting them back in to a template again. Aside from feeling dirty, what happens if I accidentally break something or forget a step?
Of course this stance means that I'm left with two choices:
- Accept that updates VMs made from my aging templates will take progressively longer and that they won't be very secure when I clone them.
- Build new templates more often so that they're current.
Neither option sounds very appealing when written like that.
Enter Packer. Its job is to eliminate the first choice and automate the second. On top of that, you can bring standardisation to your templates and the VMs they are used to build. Yes, you'll still have to patch / maintain the VMs down the road, Packer doesn't solve that problem. But it can start you off in the right place and save you time in the long run.
The process for me to build a template, say Windows Server 2019, used to be as follows:
- Create a new VM in vCenter
- Select the guest operating system
- Change the storage adpter to ParaVirtual SCSI
- Change the network adapter to vmxnet3
- Attach the Windows ISO file
- Power on the new VM
- Select to install Windows
- Enter a license key
- Detach the Windows ISO and attach a VMtools ISO
- Load the PV SCSI driver
- Detach the VMtools ISO and re-attach the Windows ISO
- Proceed with the installation
- Set an administrator password
- Mount the VMtools ISO again and install VMtools
- Run Windows Update one or more times until there are no more updates to install
- Shut the VM down
- Convert it to a template
And that just gives you the most vanilla of templates. With Packer I have a single shell script (I'm too lazy to type the packer command / options) and without any further intervention from me, I have not one but two templates pop out the other end. (It's two because I can now easily build and patch both Windows 2019 Standard AND Windows 2019 Standard Core without breaking a sweat!)
Packer is a standalone utility that can be executed from Linux, Windows, Mac etc and build in one or many endpoints (vSphere, AWS, Azure etc). My immediate requirement was to create vSphere templates for my homelab but this process could easily be adapted for the Enterprise (in fact I know a good many who have already).
To keep it in my homelab, I stood up a quick CentOS server where I could pull files from GitHub and execute packer. But you could run it on your laptop, desktop etc if you wanted.
In the next posts in this series I'll go through how I setup my packer "server", the required config files and the builds I've created.
Other posts in this series:
- Introduction <-- $this
- Configuring Packer server and required files
- Variables, builders and provisioners
- vSphere variables
- Windows Server templates
- CentOS templates
- Further plans (including scheduling packer)