Packer Build Ubuntu 22.04 for VMware vSphere

Yesterday, Ubuntu released the latest in the line of LTS versions of Ubuntu Server and Workstation. Ubuntu 22.04 is a great release featuring many new features and enhancements to the Ubuntu family of operating systems. If you are like me, you will be looking at rolling out the new Ubuntu 22.04 version of the OS with your new deployments going forward. In the home lab and in production environments, I like to deploy new virtual machines using a combination of Packer templates and Terraform for the actual deployment. In this post, we will take a look at a Packer build Ubuntu 22.04 for VMware vSphere and see how you can use Packer to spin up a new Ubuntu 22.04 template.

Packer build architecture and files needed

There are only four files needed for the packer build of Ubuntu 22.04. However, there are really only three files that have contents as I will detail below. These are the following:

  • variables.json
  • ubuntu-22.04-live-server-packer.json
  • user-data and meta-data

Let’s take a look and see what you need to do to populate these files to build out your Ubuntu 22.04 virtual machine with Ubuntu 22.04.

The variables.json file

The variables.json file contains your VMware vSphere variables that include credentials, datastore, folder, template name, cluster, network, and other configuration. This is how Packer connects and interacts with your vSphere environment.

    "username":"[email protected]",
    "folder": "Templates",
    "vm_name": "ubuntu2204_template",
    "cluster": "clustername",
    "network": "yourvSpherenetworkname"

The ubuntu-22.04-live-server-packer.json file

The next JSON file is the file housing the specific configuration for the Ubuntu configuration in Packer. This includes things like:

  • The ISO media
  • Checksum for install media
  • Password
  • Disk size
  • Disk provisioning type
  "builders": [
      "CPUs": 4,
      "RAM": 4096,
      "boot_command": [
        "linux /casper/vmlinuz --- autoinstall ds=\"nocloud-net;seedfrom=http://{{.HTTPIP}}:{{.HTTPPort}}/\"<enter><wait>",
        "initrd /casper/initrd<enter><wait>",
      "boot_wait": "3s",
      "cluster": "{{ user `cluster` }}",
      "convert_to_template": true,
      "datastore": "{{ user `datastore` }}",
      "folder": "Templates",	  
      "disk_controller_type": "pvscsi",
      "guest_os_type": "ubuntu64Guest",
      "http_directory": "./http",
      "insecure_connection": "true",
      "iso_checksum": "sha256:84aeaf7823c8c61baa0ae862d0a06b03409394800000b3235854a6b38eb4856f",
      "iso_urls": [
      "network_adapters": [
          "network": "{{ user `network` }}",
          "network_card": "vmxnet3"
      "password": "{{ user `password` }}",
      "shutdown_command": "sudo shutdown -P now",
      "ssh_username": "ubuntu",
      "ssh_password": "ubuntu",
      "ssh_handshake_attempts": "100",	  
	  "ssh_timeout": "20m",
      "ssh_port": 22,
      "storage": [
          "disk_size": 40960,
          "disk_thin_provisioned": true
      "type": "vsphere-iso",
      "username": "{{ user `username` }}",
      "vcenter_server": "{{ user `vcenter_server` }}",
      "vm_name": "{{ user `vm_name` }}"

User-data and meta-data files

Out of the two files (user-data and meta-data) only the user-meta file has contents. Create a directory called HTTP in your working directory. This HTTP directory will house the user-data and meta-data files. To help visualize this, below is the tree view of the files and HTTP directory of my working directory when setting up the files in this tutorial.

Tree view of the files needed for Packer build Ubuntu 22.04 in VMware vSphere
Tree view of the files needed for Packer build Ubuntu 22.04 in VMware vSphere

Below is the code for the user-data file.

  version: 1
    geoip: true
    preserve_sources_list: false
    - arches: [amd64, i386]
    - arches: [default]
  - default
  - name: ubuntu
    lock_passwd: false
    passwd: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
  - path: /etc/ssh/sshd_config
    content: |
         Port 22
         Protocol 2
         HostKey /etc/ssh/ssh_host_rsa_key
         HostKey /etc/ssh/ssh_host_dsa_key
         HostKey /etc/ssh/ssh_host_ecdsa_key
         HostKey /etc/ssh/ssh_host_ed25519_key
         UsePrivilegeSeparation yes
         KeyRegenerationInterval 3600
         ServerKeyBits 1024
         SyslogFacility AUTH
         LogLevel INFO
         LoginGraceTime 120
         PermitRootLogin yes
         StrictModes no
         RSAAuthentication yes
         PubkeyAuthentication no
         IgnoreRhosts yes
         RhostsRSAAuthentication no
         HostbasedAuthentication no
         PermitEmptyPasswords no
         ChallengeResponseAuthentication no
         X11Forwarding yes
         X11DisplayOffset 10
         PrintMotd no
         PrintLastLog yes
         TCPKeepAlive yes
         AcceptEnv LANG LC_*
         Subsystem sftp /usr/lib/openssh/sftp-server
         UsePAM yes
         AllowUsers ubuntu
    hostname: ubuntu2204
    username: ubuntu
    password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
    allow-pw: true
    install-server: true
    disable_root: false
  locale: en_US
    layout: en
      name: direct
      - type: disk
        id: disk0
          size: largest
      - type: partition
        id: boot-partition
        device: disk0
        size: 500M
      - type: partition
        id: root-partition
        device: disk0
        size: -1
    - 'sed -i "s/dhcp4: true/&\n      dhcp-identifier: mac/" /target/etc/netplan/00-installer-config.yaml'
    - echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu

Create a meta-data file in the HTTP directory. It will have no contents.

You can clone the Github repository here:

After getting all the files in place, you can run the packer build command:

packer build -var-file .\variables.json .\ubuntu-22.04-live-server-packer.json
Running packer build Ubuntu 22.04
Running packer build Ubuntu 22.04

Packer build FAQs

  • What is Packer? Packer is a free utility available from Hashicorp that provides an automated way to build templates from scratch in an automated way. Hashicorp Packer just requires you to feed it the configuration language for the template you want to build. Then, it will build the image you want, including Linux images, and convert these to templates in a VMware vSphere environment.
  • How do you use Packer? Packer is a simple command-line tool. Once you have the configuration files in place, you pass these configuration files to Packerin the command line, and it will build the image.
  • Is Packer the same as Terraform? No. They are two separate tools. Packer focuses on building images to deploy. Terraform is an Infrastructure-as-Code tool that allows easily creating a declarative state for your infrastructure. It will build out the infrastructure to “look” like the IaC code.
  • Can you use Packer to build Linux templates? Yes, as shown in the walkthrough, you can easily build Linux templates using Packer.

Wrapping Up

I hope this walkthrough and code examples of Packer Build Ubuntu 22.04 for VMware vSphere will help any who want to automate the process of creating automated template builds for their VMware vSphere environment.

Back to top button