Create Linux VM Customised with Cloud-Init

June 18, 2020

by Chris Rasmussen

Intended Audience Level: Beginner/Intro

Code Sample Type: Snippet

Nutanix Technologies: Prism Central

Minimum Product Version: 5.15

Script/Code Language: JSON Payload

REST API Sample? Yes

REST API Version: v3

JSON payload to create a detailed AHV VM that is then configured with Cloud-Init. All important VM parameters are specified, including base64-encoded Cloud-Init YAML spec.

Code Sample Details

Note this section may be empty, if additional code sample details are not available.

Request Parameters

The below parameters should be used with the sample payload.

Please note you will need to alter {{placeholder}} values to match your environment, e.g. subnet name and UUID, cluster name and UUID.

Request URL:

https://[prism_central_ip_address]:9440/api/nutanix/v3/vms

Request method:

POST

Prerequisites

The AHV image matching “{{cloud_init_image_uuid_pc}}” must have the Cloud-Init package installed before the Lin ux VM can be created from it.

Recommended Reading

Before using this code sample, it is strongly recommended to read the article titled “Nutanix API v3 – Creating a Linux VM with Cloud-Init“. It contains some detail about Cloud-Init and why it may be used.

Payload

{
	"spec":{
		"name":"{{vm_name}}",
		"resources":{
			"power_state":"ON",
			"num_vcpus_per_socket":1,
			"num_sockets":1,
			"memory_size_mib":1024,
			"disk_list":[{
				"device_properties":{
					"device_type":"DISK",
					"disk_address": {
						"device_index": 0,
						"adapter_type": "SCSI"
					}
				},
				"data_source_reference": {
					"kind": "image",
					"uuid": "{{cloud_init_image_uuid_pc}}"
				}
			},
			{
				"disk_size_mib":1024,
				"device_properties":{
					"device_type":"DISK"
				}
			},
			{
				"device_properties":{
					"device_type":"CDROM"
				}
			}],
			"nic_list":[{
				"nic_type":"NORMAL_NIC",
				"is_connected":true,
				"ip_endpoint_list":[{
					"ip_type":"DHCP"
				}],
				"subnet_reference":{
					"kind":"subnet",
					"name":"{{subnet_name}}",
					"uuid":"{{subnet_uuid}}"
				}
			}],
			"guest_tools":{
				"nutanix_guest_tools":{
					"state":"ENABLED",
					"iso_mount_state":"MOUNTED"
				}
			},
			"guest_customization": {
				"cloud_init": {
					"user_data": "I2Nsb3VkLWNvbmZpZwp1c2VyczoKICAtIG5hbWU6IG51dGFuaXgKICAgIHNzaC1hdXRob3JpemVkLWtleXM6CiAgICAgIC0gc3NoLXJzYSBLRVkKICAgIHN1ZG86IFsnQUxMPShBTEwpIE5PUEFTU1dEOkFMTCddCiAgICBncm91cHM6IHN1ZG8KICAgIHNoZWxsOiAvYmluL2Jhc2gKCnBhY2thZ2VzOgogIC0gaHR0cGQ="
				},
				"is_overridable": false
			}
			
		},
		"cluster_reference":{
			"kind":"cluster",
			"name":"{{cluster_name}}",
			"uuid":"{{cluster_uuid}}"
		}
	},
	"api_version":"3.1.0",
	"metadata":{
		"kind":"vm"
	}
}

SSH PUBLIC KEY

Copy the SSH public key below.  In BASH shell environments, for example, this file could be saved to ~/.ssh/nutanix_demo.pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCm+7N2tjmJw5jhPmD8MS6urZQJB42ABh73ffGQSJ0XUHgdEDfjUDFkLK0wyJCe0sF5QJnh07UQn0F0BUnBi+VwehPGeODh6S43OP5YS/14L0fyntFI06B9lckx/ygRNu82sHxXCX+6VVUFPOPC+sz6j1DQswKY9d4cEYnaMBGSzqRxrqAIf6aWIKTJTYKPFY0zaUZ6ow2iwS0Nlh5EqaXsEBWkqMmr7/auP9GV/adUgzFrGLJklYBdfH575SIK6/PZL6wNT0jE9LmFlEm7dI01ZWPclBuV16FzRyrnzmWr/ebY62A04vYBtR0vyfEfsW2ZgxgD6aAE6+ytj0v19y0elRtOaeTySN/HlXh7owKWCHnlXNpTUiSDP8SQ8LRARkhQu3KEDL0ppGCrSF87oFkp1gPzf92U+UK3LaNMMjZXMOy0zLoLEdLtbQo6S8iHggDoX4NI4sWWxcX0mtadvjy/nIOvskk9IXasQh0u0MT9ARQY5VXPluKDtEVdeow9UbvgJ1xxNkphUgsWjCiy+sjgapsuZvWqKM6TPT1i24XYaau+/Fa0vhjLb8vCMWrrtkRwGt4re243NDYcYWTzVZUFuUK0w1wqt77KgjCCeyJdsZNwrh15v780Fjqpec3EGVA0xyNbF0jn/tsnYy9jPh/6Cv767EratI97JhUxoB4gXw== no-reply@acme.com