Deploy and customize multiple Linux VMs on Nutanix AHV

A while back I wrote a blog post Nutanix AHV and Linux VM Guest Customization where I used cloudinit to customize a Linux virtual machine (VM) after deployment. Based on that I have been asked how to do this when you’ll like to deploy multiple VMs same time. For testing purposes i have used a deployment script running on any Nutanix Controller Virtual Machine (CVM) in the Nutanix cluster.

The deployment script (called cloudinit) calls a cloud init input file (cloudinitinputfile) who takes care of the VM guest customisation. Link to cloudinit file and to and cloudinitinputfile.

Follow the below steps to deploy and customize multiple Linux based VMs on AHV and customize

  • Create clouding directory on CVM
    • mkdir /home/nutanix/cloudinit
  • Paste the cloudinit script in a file called cloudinit
  • Make adjustments to the following script sections
    • basevm – The VM to be cloned
    • vmprefix – name of the VMs provisioned by the script. The script will add a number at the end of the name.
    • numvms – Number of VMs to be deployed
    • cloudinitfilevmname – Name used in the cloudinitinputfile listed as hostname and fqdn. The cloudinitfilevmname will be changed for vmprefix name for each VM deployed to guarantee a unique hostname in each VM.
    • container – Container where the VMs are deployed.
  • Paste the Cloudinitinput file listed here in a file called cloudinit input file.
  • Run the script by invoking the following command:
    • /home/nutanix/cloudinit/cloudinit

Below is the output from the command where i created 4 VMs using the script.

Cloudinit Script

# Script to deploy Linux VMs on Nutanix AHV
# Author: Magnus Andersson - Staff Solutions Architect Nutanix
# ---------------------------------------------------
# Start of user input parameters section
# Define the VM/Template that will be used as the clone source
# Define target VM naming convention, a numeric value will automatically be added to the VM name e.g. specify demo and the VMs will be named demo01, demo02 and so on
# Specify number of VMs to be created
# Specify the cloudinit input file
# Specify cloudinit file VM name. This is the name that already exists in the file pointed out on the line above
# Specify target container
# End of user input parameters section
# ----------------------------------------------------
# ---------------------------------------------------
# Start of enable extensive logging section. Remove the comments to enable additional output
#echo ----------------------------------------
#echo The extensive logging information starts here:
#echo The basevm name is: $basevm
#echo The vmprefix name is: $vmprefix
#echo The number of VMs to be created are: $numvms
#echo The cloudinit configuration file is: $cloudinitfile
#echo The name of the vm specified in the cloudinit configuration file is: $cloudinitfilevmname
#echo The name of the target container is: $container
#echo The extensive logging sections ends here
#echo ----------------------------------------
# End of enable extensive logging section
# ---------------------------------------------------
# Create the VMs
for i in `seq 1 $numvms`;
                echo ----------------
                /bin/sed -i "s/$cloudinitfilevmname/$vmprefix$i/g" $cloudinitfile
                echo Creating VM: $vmprefix$i
                /usr/local/nutanix/bin/acli uhura.vm.clone_with_customize $vmprefix$i clone_from_vm=$basevm cloudinit_userdata_path=file://$cloudinitfile container=$container
                echo --------
                /bin/sed -i "s/$vmprefix$i/$cloudinitfilevmname/g" $cloudinitfile
                echo Starting VM: $vmprefix$i
                /usr/local/nutanix/bin/acli vm.on $vmprefix$i
                echo --------

Cloudinit Input File

hostname: vmname
fqdn: vmname.domain.local

This script was tested on AOS 5.0.2 and AHV 20160925.43