Terraform: creating an Ubuntu 22 template and then guest VM in vCenter

In this article I will demonstrate how to create an Ubuntu 22 template in vCenter.  Then use Terraform to create a vSphere VM based on this template.

The VM template creation is done by manually stepping through the Ubuntu server ISO installation wizard, followed by a set of preparation steps.

Then Terraform is used to provision a VM with the customization parameters that tailor it to the environment.

Upload Ubuntu server ISO to vCenter

Download the Ubuntu 22 server ISO to your local disk.  This file is ~1.4Gb, and unfortunately there is no longer a minimal “mini.iso” for Ubuntu 22 (it was discontinued after Ubuntu 20).

Then login to vCenter, and select your main datastore, then the “Files” tab.  Press “Upload Files” and select your locally downloaded ISO file.  A progress bar will display until complete.

Create VM based on Ubuntu ISO

Create a folder for templates in vCenter:

  • From vCenter, go to “Hosts and Clusters” and right-click on your vCenter cluster or esxi host
  • Go to the “VMs and Templates” tab on the left
  • Right-click your datacenter or cluster, right-click and select “New Folder” > New VM and Template folder, name=”templates”

Create a vCenter VM that will become a template:

  • From vCenter, go to the “VMs and Templates” tab on the left
  • Right-click your “templates” folder created above, select “New Virtual Machine”
  • Create a new virtual Machine, next
  • Virtual machine name=”ubuntu-2204″, the “templates” folder will already be selected, next
  • Select a compute resource, next
  • Select a storage location, next
  • Compatible with your version (e.g. ESXi 6.7 or later), next
  • Guest OS=Linux, version=Ubuntu 64 bit, next
  • Configure VM, cpu=1, mem=1Gb, Hard disk=60Gb, network=VM Network, DVD=datatstore ISO file, then select the ISO you uploaded earlier, next
  • Finish

Follow Ubuntu server installation process

Go to “VMs and Templates” tab on the left, select your VM “ubuntu-2204”, then “Edit Settings”.  The CD/DVD drive will not have the “Connect” checkbox checked, enable that checkbox and press OK.

Then power on the VM and “Launch Web Console” and you should see that you are entered into the standard wizard steps of installing Ubuntu 22 Jammy.

This series of installation wizard screens is very standard so is documented in several locations throughout the web with screenshots [1,].  Here are the descriptions of each screen and how to populate:

  • language=English
  • keyboard layout=english, variant=english
  • base installation=Ubuntu Server
  • network
    • choose ‘ens’ connection with DHCP
    • If DHCP autoconfig fails then setup manually according to vCenter network selected
  • proxy (leave empty)
  • mirror (leave default US)
  • Disk, Guided – use entire disk and setup LVM
    • use entire disk (/dev/sda)
    • setup this disk as an LVM
    • summary shown, Continue
  • profile setup
    • your name=ubuntu
    • server name=ubuntu-2204
    • username=ubuntu
    • password=ExamplePass@456
  • Install OpenSSH server=checked, import ssh identity=no
  • looking for 3rd party drivers, continue
  • featured server snaps (do not check any)
  • …Installing system…. (takes about 20 minutes)
  • Installation complete, press ‘Reboot Now’

Then from vCenter:

  • Select the VM in vCenter and Power > Power Off
  • Select “Edit Settings” and uncheck the CD/DVD checkbox so it disconnects
  • Power > Power On
  • Launch Web Console

Post config steps to prepare for templating

Now we need to run some housekeeping and preparation steps to get this ready to be a VM template.

Login as ‘ubuntu’ with password ‘ExamplePass@456’ like we provided in the installation wizard.

I have provided a script prepare-ubuntu-22.04-template.sh.  Do not run this from your host VM, run it only on the vSphere VM being prepared as a template!

# download and run
wget https://raw.githubusercontent.com/fabianlee/tf-vsphere-singlevm-from-template/main/on_template_only/prepare-ubuntu-22.04-template.sh
/bin/bash ./prepare-ubuntu-22.04-template.sh

# shutdown VM
sudo shutdown -h now

Convert to template

From vCenter, select the “ubuntu-2204” VM.  Right-click and select Template > Convert to Template.

At this point, you could manually create VMs from this template in the vCenter web GUI.  But you would want to create a VM customization specification (Menu > Policies and Profiles) so that you could provide the network values.

But since we want to use this from Terraform, we can have Terraform do all the customizations of hostname, DNS, and networking.  So let’s move on to the next section.

Install Terraform

If you have not installed Terraform, follow my instructions here.

Pull Terraform project from github

Pull the necessary Terraform files from my github project tf-vsphere-singlevm-from-template

# get OS packages required
sudo apt install git make -y

# clone project
git clone https://github.com/fabianlee/tf-vsphere-singlevm-from-template.git

cd tf-vsphere-singlevm-from-template

# edit files per your environment
# customize vcenter connection, networks, jumphost name and network
vi terraform.22.tfvars

Create VM from template using Terraform

Now run terraform and create the VM within vSphere using the template created earlier.

# get plugins
terraform init

# create
terraform apply -var-file=terraform.22.tfvars

 

REFERENCES

Ubuntu 22.04 downloads

kublr.com, prepare ubuntu template with cloud-init for vsphere

sh0rez, deploying ubuntu cloud images to vsphere

govc command reference

vmware, inserting metadata and userdata into already uploaded ova

linoproject, terraform the vsphere cloud-init

fabianlee.org, fixing and cloning an Ubuntu host

terraform, vsphere_virtual_machine resource

github terraform issue, good explanation by tmashos of chain for how metadata is passed from terraform-vsphere-cloud-init

blahcloud.com, advice at bottom on trouble spots and troubleshooting cloud-init

github tierre, bash script for preparing Ubuntu 20.04

github jimangel, bash script for preparing Ubuntu 18.04

notch.org, using govc to set userdata(cloud_config) and metadata on a vm

alexdess.com, govc to pull and modify ova specification for template

NOTES

installing the vmware remote console bundle

If you have issues with vmware web console and keyboard input, try the “VMware Remote Console” instead. The download link is available from https://esxi1.home.lab/ui.

sudo ./VMware-Remote-Console-12.0.1-18113358.x86_64.bin

Appending multi-line heredoc as sudo

export my_squid=squid.private:3128

# use proxy for apt
sudo tee /etc/apt/apt.conf.d/00proxy <<EOF
Acquire::http::Proxy "http://$my_squid";
Acquire::https::Proxy "http://$my_squid";
EOF