Skip to content
Luca Dell'Oca Principal Cloud Architect @Veeam
Virtual To The Core Virtual To The Core

Virtualization blog, the italian way.

  • Media
  • About me
Virtual To The Core
Virtual To The Core

Virtualization blog, the italian way.

My Automated Lab project: #2 Create a Ubuntu template in VMware vSphere with Packer

Luca Dell'Oca, September 26, 2023September 23, 2023

In my previous post I explained the tools I use in my lab automation. Today, we’ll talk about creating Ubuntu templates into VMware vSphere. For this, I will use Packer.

NOTE: You can grab all the files that I’m presenting here from my Github repo:
https://github.com/dellock6/packer
Some parameters in the public files have been anonymized, please customize the values to suit your needs.

Prepare the system

Before being able to run Packer, I need to create its scripts, but even before I need the data that I will put into the scripts.
Let’s start with the ISO file from where I will install Ubuntu 22.04. I grab the needed Ubuntu ISO and I save it in a vSphere library. I usually get the “live server” version, as it has all the packages available in the ISO. I prefer this method compared to the direct download of live ISO each time I run the script (a common solution that i’ve seen in many similar articles) because it’s faster to run after I only grabbed the ISO once, and it can work also when there is some connectivity issue.
This is the content of my library:
but what I need is its physical path in the underlying datastore, that I can read by browsing the datastore itself:
This information will be passed as a variable in the Packer script, like this:
iso_path = "[vsanDatastore] contentlib-5ed427e9-8cf4-442a-8218-14a8d3cc9dc3/ab0bf964-474e-4e4d-90dd-a324b392d660/ubuntu-22.04.2-live-server-amd64_1641f4b9-dbe1-409a-9433-7cd39c9dbfe7.iso"

I do this because I didn’t find a way to pass the library information to Packer, while I can mount an ISO from a datastore.

Passwords and other data

This is the structure of the files used by Packer:

I need to edit some of them to enter the data I need to customize the VM I will create. Let’s start from top to bottom.

user-data

The user-data file is in the http folder along with an empty file called meta-data. This meta-data file is required for cloud deployments, but since I’m not deploying to the cloud I can leave it empty. But I need to customize the user-data file in some points:
#cloud-config
autoinstall:
    version: 1
    early-commands:
        # workaround to stop ssh for packer as it thinks it timed out
        - sudo systemctl stop ssh
    locale: en_US
    keyboard:
        layout: en
        variant: us
    packages: [open-vm-tools, openssh-server, net-tools, network-manager, perl, open-iscsi, ntp, curl, vim, ifupdown, zip, unzip, gnupg2, software-properties-common, apt-transport-https, ca-certificates, lsb-release, python3-pip, jq, cloud-init]
    network:
        network:
            version: 2
            ethernets:
                ens192:
                    dhcp4: true
    identity:
        hostname: ubuntu2204
        username: ubuntu
        password: "$6$rounds=4096$pwAk.wxvgujkbm$4x724AhMsslbqtp4dISZppC8sHsNmafcVl.R0B0qZ2q8i2TH4/x4uWfpz1JOX3glSMPo4leBoxn7Oyr62U3In."
    ssh:
        install-server: yes
        allow-pw: yes
        authorized-keys:
            - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCb7fcDZfIG+SxuP5UsZaoHPdh9MNxtEL5xRI71hzMS5h4SsZiPGEP4shLcF9YxSncdOJpyOJ6OgumNSFWj2pCd/kqg9wQzk/E1o+FRMbWX5gX8xMzPig8mmKkW5szhnP+yYYYuGUqvTAKX4ua1mQwL6PipWKYJ1huJhgpGHrvSQ6kuywJ23hw4klcaiZKXVYtvTi8pqZHhE5Kx1237a/6GRwnbGLEp0UR2Q/KPf6yRgZIrCdD+AtOznSBsBhf5vqcfnnwEIC/DOnqcOTahBVtFhOKuPSv3bUikAD4Vw7SIRteMltUVkd/O341fx+diKOBY7a8M6pn81HEZEmGsr7rT ubuntu@ubuntu.local
    storage:
        layout:
            name: direct
    user-data:
        disable_root: false
    late-commands:
        - echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu
        - curtin in-target --target=/target -- chmod 440 /etc/sudoers.d/ubuntu
As you can read, I configure here the keyboard layout first; then I add the list of the packages I want to automatically install: open-vm-tools are obviously paramount to have the VM be able to use the virtual hardware; openssh-server will be used to connect to the VM from remote and configure it, plus other programs for the networking part and other needs.
For the network, I will use DHCP: there is a way to configure a VM with a static IP, but this is a template so I don’t want to stick with a fixed IP address. Obviously, to create this VM I will need a DHCP server listening on the same portgroup where I will connect this VM.
In the identity section I configure the hostname and I create a default ubuntu user. The password is not written in clear text, but it’s hashed using the following procedure.
I install whois (since mkpasswd is part of this package) and then I input the password so that mkpasswd will return its hash:
sudo apt-get install whois 
mkpasswd -m sha-512 --rounds=4096
In the authorized-keys section, I can insert my own SSH key that I have in my management Linux machine, the one I’ll use to connect later to the VM. I can create my own SSH key by using:
ssh-keygen

or, if I already have it, I can read it using:

cat ~/.ssh/id_rsa.pub

ubuntu2204.pkr.hcl

This is the main packer file that I will use it to build my machine. There is nothing to configure here. It’s however interesting to notice that user-data is mounted as a cd-rom to be used in the configuration:
cd_files = [
      "./${var.http_directory}/meta-data",
      "./${var.http_directory}/user-data"]

Also, in the build section I invoke a specific builder for VMware vSphere:

build {
  sources = [
    "source.vsphere-iso.linux-ubuntu-server"]

vsphere.pkrvars.hcl

This is the file where I store all the vSphere information:
##################################################################################
# VARIABLES
##################################################################################
# Credentials
vcenter_username                = "administrator@vsphere.local"
vcenter_password                = "password"
# vSphere Objects
vcenter_insecure_connection     = true
vcenter_server                  = "vcenter.vsphere.local"
vcenter_datacenter              = "Datacenter"
vcenter_host                    = "esx.vsphere.local"
vcenter_datastore               = "vsanDatastore"
vcenter_network                 = "VM Network"
vcenter_folder                  = "Templates"
# ISO Objects
iso_path                        = "[vsanDatastore] contentlib-5ed427e9-8cf4-442a-8218-14a8d3cc9dc3/ab0bf964-474e-4e4d-90dd-a324b392d660/ubuntu-22.04.2-live-server-amd64_1641f4b9-dbe1-409a-9433-7cd39c9dbfe7.iso"

Obviously, you will need to adjust these values to be compatible with your own environment.

ubuntu2204.pkrvars.hcl

This is the file where I store all the VM information:
##################################################################################
# VARIABLES
##################################################################################
# HTTP Settings
http_directory = "http"
# Virtual Machine Settings
vm_name                     = "ubuntu2204"
vm_guest_os_type            = "ubuntu64Guest"
vm_version                  = 17
vm_firmware                 = "bios"
vm_cdrom_type               = "sata"
vm_cpu_sockets              = 1
vm_cpu_cores                = 2
vm_mem_size                 = 4096
vm_disk_size                = 20480
thin_provision              = true
disk_eagerly_scrub          = false
vm_disk_controller_type     = ["pvscsi"]
vm_network_card             = "vmxnet3"
vm_boot_wait                = "5s"
ssh_username                = "ubuntu"
ssh_password                = "ubuntu"
# ISO Objects
iso_file                    = "ubuntu-22.04.1-live-server-amd64.iso"
iso_checksum                = "10f19c5b2b8d6db711582e0e27f5116296c34fe4b313ba45f9b201a5007056cb"
iso_checksum_type           = "sha256"
iso_url                     = "https://releases.ubuntu.com/jammy/ubuntu-22.04.1-live-server-amd64.iso" 
# Scripts
shell_scripts               = ["./scripts/setup_ubuntu2204.sh"]

Here I customize the virtual machine I’m going to build.

Build the VM

I start by initializing the Packer environment:
packer init .

This will verify that the system is ready to execute my scripts; on the first run it will install all the needed additional packages.

Once it’s ready, I start the building script:
packer build -force -on-error=ask -var-file ubuntu2204.pkrvars.hcl -var-file vsphere.pkrvars.hcl ubuntu2204.pkr.hcl

Let’s go grab a coffee, and let Packer work on this:

After a few minutes, if all worked correctly, it should end up with a screen like this:
I have built my new Ubuntu 22.04 template. It took 17 minutes, but I only spent a few seconds running the command, and I’ve used the rest of the time to do something else. And every time I need to update the template, I will just destroy it and build a new one.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Pocket (Opens in new window) Pocket
Tech ansibleautomationcodehcliacinfrastructurelabpackertemplateterraformubuntuuser-datavsphere

Post navigation

Previous post
Next post

Search

Sponsors

Latest Posts

  • Migrate WSL (Windows Subsystem for Linux) to a new computer
  • Pass keystrokes to a pfSense virtual machine to install it automatically
  • Automatically deploy pfSense with Terraform and Ansible
  • My Automated Lab project: #6 Create a S3 Bucket with Terraform
  • My Automated Lab project: #5 Deploy a Linux vSphere VM with Terraform and custom disks
©2025 Virtual To The Core | WordPress Theme by SuperbThemes
We use cookies to ensure that we give you the best experience on our website, and to collect anonymous data regarding navigations stats using 3rd party plugins; they all adhere to the EU Privacy Laws. If you continue to use this site we will assume that you are ok with it.OkNoPrivacy Policy