The Adventure of Building a Customizable VM Template with Packer and vRealize Automation
As I delved into the realm of building customizable VM templates with Packer and vRealize Automation (vRA), I encountered various challenges that threatened to derail my journey. In this blog post, I will share my experiences and the solutions I discovered along the way.
Ubuntu 20.04 Boot Customization
My first hurdle was to get Ubuntu 20.04 to properly boot from the Packer HTTP directory. After scouring the internet for answers, I settled on the following command:
`packer build –boot-wait 500 –http-directory /path/to/ubuntu/20.04/iso`
This command allowed me to boot Ubuntu from the HTTP directory and customize the boot process using cloud-init.
Cloud-Init User Data File
Next, I needed to create a working user-data file for the boot customization process. After experimenting with different examples found online, I settled on a mish-mash of various commands to generate a strong password using sha-512 with 4096 rounds:
`mkpasswd –method=sha-512 –rounds=4096 `
The password in this example was `VMware1!`. This user-data file would be used to customize the boot process and set up the VM for later use.
Preparing the Template
After cloud-init is finished, I wanted to have a “prep” script run to clean everything up and prepare the template for usage. This script would run during the build process and would perform various tasks such as setting the hostname, creating a default user, and installing any necessary packages.
VMware Guest Info Plugin
One of my first stumbling blocks was deploying a VM from this template with a cloud-config specification that would never be customized. No matter what I tried, even something as basic as the hostname was not being set. After much research, I discovered that installing the custom VMware Guest Info plugin for Cloud Init by running this installation script during the template build was recommended.
However, even after trying this approach, I still had no luck. That is until I noticed that the install script repository for the VMware Data source had been archived! After taking a closer look at the VMware Guest Info README, I discovered that the datasource had been merged into cloud-init as DataSourceVMware.
Adding the DataSourceVMware to my prep script allowed me to set the hostname and other customizations for the VM:
`source /path/to/cloud-init.sh`
`cloud-init config`
`cloud-init credentials`
`cloud-init clean`
`Re-running the build and testing it in vRA. Yep, hostname is now being set!`
vRealize Automation Customization
With my new template now customizing the hostname, I noticed that the default ubuntu account was being reset and I couldn’t log in with the password. After some research, I found that vRA was pushing the following cloud-init config:
`No config was being pushed for the default ubuntu user.`
To reset the default user, typically ubuntu on a fresh install, you need to add this code to your cloud-init:
`If you specify a remoteAccess user that differs to the ubuntu account, vRA will try to push another user as the default:`
In my case, I specified a remoteAccess user that differed from the ubuntu account, so vRA pushed another user as the default. This allowed me to customize the hostname and other settings for the VM without affecting the default ubuntu account.
Conclusion
Building customizable VM templates with Packer and vRealize Automation can be a challenging but rewarding endeavor. By carefully attention to the various components of the process, including cloud-init and the VMware Guest Info plugin, you can create highly customized templates that meet your specific needs. With these solutions in mind, you’ll be well on your way to creating powerful and flexible VM templates for your vRA environment.