Automation

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.

{
    "vcenter_server":"vcsa.cloud.local",
    "username":"[email protected]",
    "password":"Secretpassword",
    "datastore":"yourdatastorename",
    "folder": "Templates",
    "vm_name": "ubuntu2204_template",
    "host":"esx5.cloud.local",
    "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": [
        "<esc><esc><esc><esc>e<wait>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "linux /casper/vmlinuz --- autoinstall ds=\"nocloud-net;seedfrom=http://{{.HTTPIP}}:{{.HTTPPort}}/\"<enter><wait>",
        "initrd /casper/initrd<enter><wait>",
        "boot<enter>",
        "<enter><f10><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": [
        "iso/ubuntu-22.04-live-server-amd64.iso",
        "https://releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso"
      ],
      
      "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.

#cloud-config
autoinstall:
  version: 1
  apt:
    geoip: true
    preserve_sources_list: false
    primary:
    - arches: [amd64, i386]
      uri: http://gb.archive.ubuntu.com/ubuntu
    - arches: [default]
      uri: http://ports.ubuntu.com/ubuntu-ports
  users:
  - default
  - name: ubuntu
    lock_passwd: false
    passwd: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
  write_files:
  - 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
  identity:
    hostname: ubuntu2204
    username: ubuntu
    password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
  ssh:
    allow-pw: true
    install-server: true
  user-data:
    disable_root: false
  locale: en_US
  keyboard: 
    layout: en
  storage:
    layout:
      name: direct
    config:
      - type: disk
        id: disk0
        match:
          size: largest
      - type: partition
        id: boot-partition
        device: disk0
        size: 500M
      - type: partition
        id: root-partition
        device: disk0
        size: -1
  late-commands:
    - '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:

https://github.com/brandonleegit/PackerBuilds

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.

Subscribe to VirtualizationHowto via Email 🔔

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com and has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family.

Related Articles

4 Comments

  1. Hi there, i’m getting a ssh timeout after the “waiting for ssh to become available”. Any clue why this would fail? I’ve logged in to the vm while it’s being build and I can’t pinpoint where the problem is. Thanks!

    1. Ruedi,

      Have you connected to the console of the VM being built? Does the build seem to be progressing along?

      Brandon

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.