Customizing a VM using cloud-init in VMware Cloud Director

By using cloud-init, you can customize a new VM using code, during the creation of the VM.


Here, you will learn how to customize a VM using cloud-init in a VMware Cloud Director environment.

If you are deploying a VM from a Glesys template in VMware Cloud Director, then follow the instructions outlined in this document.

However, if you are deploying a VM from a custom-built template, then the steps outlined here do not apply, and you can continue using existing workflows for customization.

Brief overview of guest OS customization in VMware Cloud Director

Historically, VMs deployed from a Glesys template in VMware Cloud Director have been customized using the vmware-imc method, the default method supported in the platform.

The vmware-imc method leverages customization scripts bundled with VMware Cloud Director, which are copied onto the guest operating system and then executed by the vmtools service running in the guest.

From now on, VMs deployed from a Glesys template in VMware Cloud Director will be customized using cloud-init.

The cloud-init method has become the de facto industry standard for customizing VMs in the cloud. In a VMware environment, the cloud-init configuration data supplied to a VM is read using vmtools. The cloud-init service running in the guest then customizes the guest operating system according to that configuration.

There are many benefits to using the cloud-init method over the vmware-imc method:

  • cloud-init offers more capabilities with the potential to automate the entire initial setup of a VM.

  • cloud-init offers better security as it does not rely on default root/admin accounts like vmware-imc.

  • cloud-init offers broader guest operating system support. vmware-imc can be limited, and it can take a long time after a new OS is released before it is officially supported.

How to customize a VM using cloud-init

The following steps apply to all GleSYS templates in VMware Cloud Director.

Step 1 ­– Generating a VMware Cloud Director API token

  1. In the top right corner of the navigation bar, click your user name, select User preferencesAPI Tokens, and click New.

  2. Enter a Name for the token, and click Create. The generated API token appears. You must copy the token because it appears only once.

Step 2 – Creating a cloud-init metadata file

The metadata file is a plain-text file formatted as YAML, where you can define the hostname, instance ID, and network configuration of your VM.

  1. Create a metadata.yaml file on your local machine and paste the following configuration:

Step 3 – Creating a cloud-init userdata file

The userdata file is a plain-text file formatted as YAML, where you can define many options to customize your VM, including options to create user accounts, install software packages, execute commands, and create files.

  1. Create a userdata.yaml file on your local machine and paste the following configuration:

Step 4 – Creating a VM from a template

  • Navigate to ComputeVirtual Machines and click New VM.

  • Enter a Name and a Computer Name.

  • Select From Template.

  • Uncheck the Power on check box.

  • Select a VM template from the list of available templates. For this article, choose ubuntu-2204.

  • Select a Storage Policy.

  • Specify the settings for the network adapter, such as Connected, Network, and IP Mode.

  • Click OK to create the VM.

Unfortunately, supplying the cloud-init configuration in the New VM wizard is not currently possible. Therefore, an additional step is required to provide the cloud-init configuration using the VMware Cloud Director API.

Step 5 – Supplying a cloud-init configuration to a VM

  1. Supply the cloud-init configuration to the VM using the set_vcd_vm_extraconfig binary, which you can download from GitHub. Then, run the commands below on your local machine:

The above commands assume your local machine is running Linux/macOS. If your local machine is running Windows, you can run the following command in PowerShell:

Step 6 – Powering on the VM

  1. Navigate to ComputeVirtual Machines. Choose the specific VM and click ActionsPowerPower On.

You should now be able to SSH to your VM and verify that cloud-init has customized the instance according to the configuration specified in metadata and userdata.

Additional information regarding Windows virtual machines

As mentioned, all the steps outlined in this article apply to both Linux and Windows-based Glesys templates.

However, there is a difference regarding the contents of the userdata file when comparing Windows and Linux deployments that is worth highlighting.

Windows userdata file

Here is a sample userdata.yaml file for Windows-based VMs:

Please be aware that user passwords are specified in plaintext in the userdata file. The userdata configuration is concealed on the VM after creation as a security measure. However, we recommend changing the user password in the VM after its creation to ensure maximum security.

Further reading

cloud-init - Official Documentation

cloud-init - Userdata Examples

cloudbase-init (cloud-init equivalent for Windows) - Official Documentation

cloudbase-init (cloud-init equivalent for Windows) - Userdata Examples

Last updated

Was this helpful?