HashiCorp Packer to Build a Ubuntu 20.04 Image Template in VMware
- 7 minutes read- 1318 words
Tools such as Packer and Terraform from HashiCorp have been widely used for Cloud environments. However, we’ve seen lately that VMware is also getting a lot of attention. In speaking with multiple customers, we’ve seen that private cloud teams are seeing the benefits of Infrastructure as Code (IaC) workflows used in cloud environments. As a result, these private cloud teams are also implementing IaC on-premises. The goal of this blog post is to help the private cloud teams to see an example of how to automate the creation of a Ubuntu 20.04 VMware template with Packer. In a subsequent blog post, we will see how to use Terraform to provision VMs by cloning this VMware template.
Access to a vSphere instance (tested on vSphere v6.7)
Below is our setup diagram.
Let’s take a look at the most important configuration pieces needed.
Below is the structure of the repo folder.
Ubuntu Server Installer for 20.04 LTS
The new way to install Ubuntu is using something called subiquity server installer. The classic server debian-installer is discontinued. Therefore, we can’t rely on the preseed file that we used in the past. Instead, we will rely on CloudInit. Notice we used the preseed file in the Ubuntu 18.04 in our HashiCorp Packer for VMware Ubuntu 18.04 templates video.
With 20.04 LTS, we will be completing the transition to the live server installer and discontinuing the classic server installer based on debian-installer (d-i), allowing us to focus our engineering efforts on a single codebase. The next-generation subiquity server installer brings the comfortable live session and speedy install of Ubuntu Desktop to server users.
CloudInit is installed in the official Ubuntu 20.04 live server image. CloudInit uses a user-data file to configure things such as the below:
Setting a default locale
Creating a hostname
Generating ssh private keys
Adding ssh keys to a user’s .ssh/authorized_keys so they can log in
The user-data file is in the http folder along with an empty file called meta-data. This meta-data file is required. It’s used for cloud deployment, but since we are not deploying to the cloud we can leave it empty. Let’s take a look at what the user-data file looks like.
User-Data File Content
Below is the content of the file. Notice that we can install packages here. We’re also putting in the public key to be able to ssh into the machine later. We have the option to run both early and late commands. We’ve disabled ssh as an early-command because it interferes with Packer. Packer thinks that the process timed out and may result in an error.
We need to generate a hashed password for the identity section in the user-data file.
We use the mkpasswd utility on Ubuntu, but first we install the whois package to get the mkpasswd utility as shown below.
I’ve included both the hcl and the json configuration for Packer. Either one works. My preference is to move towards hcl to be consistent with the rest of the HashiCorp tools such as Terraform and Vault.
In this section, let’s focus on the hcl configuration. The file is called ubuntu-20.04.pkr.hcl. It’s pretty straightforward.
Notice the user-data and meta-data files are mounted as CDROM files below:
We also feed in variables files. One is vCenter configuration-specific called vsphere.pkrvars-example.hcl and the other is VM specific and called variables.pkrvars650GBdisk.hcl
Below is a script that is called during the provisioning phase called setup_ubuntu2004_withDocker.sh. Cleaning the machine-id is very important to make the template re-usable when cloning it to generate VMs using Terraform later.
Follow the Packer output logs to see the image successfully generated. You could also check what’s going on with the VM inside of your vSphere client.
If packer gets stuck on Waiting for IP you may want to check your DHCP server. I’m using a home router and it had too many leases from running packer many times. I had to flush inactive DHCP clients or reboot the router which is faster.
Open the vSphere web console and take a look at the output as the VM is getting created. This can give you some hints as to what is going on.
In this blog post, we demonstrated how to create a Ubuntu 20.04 image in VMware using HashiCorp Packer. As you saw, our configuration is all defined in code. There is no need to click around inside the vSphere client to generate this image. We’ve automated the task of building gold images in VMware. This makes the process repeatable and self-documented, just a couple of benefits of IaC. The next step is to use Terraform to provision VMs by cloning this Packer generated image.