In the last chapter we set up Packer and built our Rocky and Ubuntu templates. Now say Ubuntu 26.04 is out, and we want to deploy some Ubuntu 26.04 VMs. Let’s see how it’s done, and how to change Terraform without touching the VMs that are already running.
The whole job is one new values file in Packer and one new line in Terraform.
Part 1: Packer
As part of the procedure, I’ve already download the Ubuntu 26 and placed into my ISO folder

Go to the Packer Ubuntu folder and create a new values file for 26.04 as ubuntu2604.pkrvars.hcl. The iso_path must match the ISO’s name and location on the datastore exactly.
vm_name = "tpl-ubuntu-2604"
guest_os_type = "ubuntu64Guest"
iso_path = "[nfs-iso] ubuntu-26.04.1-live-server-amd64.iso"
build_ip = "172.16.11.252"
ubuntu2604.pkrvars.hcl holds the Ubuntu 2604 values: template name, OS type, ISO, repo URLs, temp build IP

That’s all we need to do at Packer end. Now that we have all the required configurations in place we will progress with build.
Build the template
To start the build, navigate to the following folder – cd /etc/ansible/packer/ubuntu

Authenticate to vCenter.
source /etc/ansible/terraform/env.shInitialise packer on the folder using packer init .
packer init .
Validate the 2604 config file using packer validate
packer validate -var-file=../vsphere.pkrvars.hcl -var-file=ubuntu2604.pkrvars.hcl .Build the VM template using packer build command
packer build -force -on-error=ask -var-file=../vsphere.pkrvars.hcl -var-file=ubuntu2604.pkrvars.hcl .
Wait a few minutes while Ubuntu installs itself, reboots, and the clean-up runs

At the end the VM converts itself into the template tpl-ubuntu-2604, ready to use. That’s an unattended installation with Packer.

Part 2: Terraform
Now the fun part. We can’t use the new template straight away; Terraform has to be told about it first.
This is my current terraform.tfvars.

All these VMs are on Ubuntu 24.04, because they don’t set os, so they use the default, ubuntu:

Tell Terraform about the new template
To build a new VM with the latest image on Ubuntu24 we need few tweaks to be done to Terraform
Go to data.tf. The current templates table looks like this:

Add an extra entry for the new version, with a new name on the left:
Left side (ubuntu26): a short name of your choice. It must be unique
Right side (tpl-ubuntu-2604): must match the template name in vCenter exactly
Don’t change the existing ubuntu line to point at the new template. Every VM using ubuntu would then look like it comes from a different template, and the next Terraform run would want to destroy and rebuild them all. That’s because Terraform remembers which template each VM was cloned from, by ID. If that ID changes, Terraform treats it as a different VM so Never repoint an existing name. Add a new name for each new template.

That’s all Terraform needs to know about the new template.
Build a VM from it
In terraform.tfvars, add the new VM with os = "ubuntu26":

Now let’s run terraform plan and see what happens. So, it says 1VM to be added and nothing to destroy

Run terraform plan -out=tfplan to add the plan to our save so this shows +1 VM to be added and no changes are going to be done on exisiting VM.. One VM to add, and nothing changed or destroyed on the existing VMs. If it says anything else, stop and find out why before applying.

Finally, apply the saved plan using terraform apply “tfplan”

That’s how you add a new OS version to Packer and then Terraform and build VMs from it safely.

Note – Never change the current name of the template in the terrform as it will cause all VM to be replaced with teh new image we aere adding so create new entry..

