Customizing a VM Using cloud-init in VMware Cloud Director

Introduction


In this article, 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, including both Linux and Windows-based ones.

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 preferences -> API 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.

VCD cloud-init - API Token

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:
instance-id: 00000000-0000-0000-0000-000000000000 # replace with your own id
local-hostname: demo.example.com # replace with your own FQDN
network:
  version: 2
  ethernets:
    ens192:
      dhcp4: yes

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:
#cloud-config
users:
- name: demo
  shell: /bin/bash
  sudo: ALL=(ALL) NOPASSWD:ALL
  lock_passwd: true
  ssh_authorized_keys: 
    - # Paste your public SSH key here
manage_etc_hosts: true

Step 4 - Creating a VM from a template


  1. Navigate to Compute -> Virtual Machines and click New VM.
  2. Enter a Name and a Computer Name.
  3. Select From Template.
  4. Uncheck the Power on check box.
  5. Select a VM template from the list of available templates. For this article, choose ubuntu-2204.
  6. Select a Storage Policy.
  7. Specify the settings for the network adapter, such as Connected, Network, and IP Mode.
  8. Click OK to create the VM.

VCD cloud-init - Create 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:
export METADATA=$(gzip -c9 <metadata.yaml | base64 -w0 )
export USERDATA=$(gzip -c9 <userdata.yaml | base64 -w0 )

./set_vcd_vm_extraconfig -url https://vcd.dc-fbg1.glesys.net \
 -token ABC12345678 -org vdo-##### -vdc vdc-##### -vm demo \
 -e guestinfo.metadata="${METADATA}" \
 -e guestinfo.metadata.encoding="gzip+base64" \
 -e guestinfo.userdata="${USERDATA}" \
 -e guestinfo.userdata.encoding="gzip+base64"

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

$metadata = [convert]::ToBase64String((Get-Content -path "metadata.yaml" -Encoding byte))
$userdata = [convert]::ToBase64String((Get-Content -path "userdata.yaml" -Encoding byte))

.\set_vcd_vm_extraconfig.exe -url https://vcd.dc-fbg1.glesys.net `
 -token ABC12345678 -org vdo-##### -vdc vdc-##### -vm demo `
 -e guestinfo.metadata="$metadata" `
 -e guestinfo.metadata.encoding="base64" `
 -e guestinfo.userdata="$userdata" `
 -e guestinfo.userdata.encoding="base64"

Step 6 - Powering on the VM


  1. Navigate to Compute -> Virtual Machines. Choose the specific VM and click Actions -> Power -> Power 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.

VCD cloud-init - Verify VM

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:

#cloud-config
users:
  - name: Administrator
    no_create_home: True
    inactive: True
  - name: demo
    groups: Administrators
    passwd: passw0rdIsPlainText

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


Hittar du inte det du söker?

Kontakta oss gärna för mer information. Vi hjälper dig att komma fram till den bästa lösningen för dina behov.

Skicka e-post Ring 0200-23 88 00