Building on the previous example, let’s use Terraform to create multiple VM’s using just one Terraform file/configuration. Let’s take the previous example’s tf file and check the options available
$ cat ntnx.tf
provider "nutanix" {
username = "username"
password = "password"
endpoint = "IP_of_PE"
insecure = true
port = 9440
}
data "nutanix_image" "centos" {
image_id = "2b4b5942-2c0e-45eb-b17e-a10b8479c824"
}
resource "nutanix_virtual_machine" "ayan-test" {
name = "ayan-test"
description = "ayan terra"
num_vcpus_per_socket = 2
num_sockets = 1
memory_size_mib = 4096
}
Now, we know that this will create one VM named”ayan-test“. However, what if we wanted to create a hundred (that’s taking it a bit too far, I guess. let’s stick to 5 instances) VMs with the same basic configuration?
Now, there are a couple of problems here
- Name of the VM needs to be unique – else we would just get a bunch load of VMs with the same name!
- NIC cards/other specifics will need to be unique (hence, left out the NIC cards in the base example for simplicity’s sake! But we will try the options later)
Now, we can use the “count” and use that as an input for both NIC cards, drives or for that matter, VM names!
https://www.terraform.io/intro/examples/count.html
Nice! So let’s modify the script as below, essentially add in a count for the number of VM’s and increment the count value as variables for entities we need to increment! Simple right? Here goes…
$ cat ntnx.tf
provider "nutanix" {
username = "username"
password = "PE_passwords"
endpoint = "PE_CVM_IP_ADDRESS"
insecure = true
port = 9440
}
data "nutanix_image" "centos" {
image_id = "2b4b5942-2c0e-45eb-b17e-a10b8479c824"
}
resource "nutanix_virtual_machine" "ayan-test" {
count = "5" <<------------------------------
name = "ayan-test-${count.index + 1}" <--------
description = "ayan terra"
num_vcpus_per_socket = 2
num_sockets = 1
memory_size_mib = 4096
}
Now that we have that, just save the file and apply. Ideally we should be seeing 5 VM operations when we plan and then apply. We can always modify if this comes back with errors
$ terraform apply
nutanix_virtual_machine.ayan-test[0]: Refreshing state... (ID: 91420eb8-19a5-4fc4-8947-8aa3369e5c96)
data.nutanix_image.centos: Refreshing state...
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
+ nutanix_virtual_machine.ayan-test[1]<--------
id: <computed>
api_version: <computed>
availability_zone_reference.%: <computed>
boot_device_disk_address.%: <computed>
boot_device_mac_address: <computed>
boot_device_order_list.#: <computed>
categories.%: <computed>
cluster_name: <computed>
cluster_reference.%: <computed>
description: "ayan terra"
enable_script_exec: <computed>
gpu_list.#: <computed>
guest_customization_cloud_init_custom_key_values.%: <computed>
guest_customization_cloud_init_meta_data: <computed>
guest_customization_cloud_init_user_data: <computed>
guest_customization_is_overridable: <computed>
guest_customization_sysprep.%: <computed>
guest_customization_sysprep_custom_key_values.%: <computed>
guest_os_id: <computed>
hardware_clock_timezone: <computed>
host_reference.%: <computed>
hypervisor_type: <computed>
memory_size_mib: "4096"
metadata.%: <computed>
name: "ayan-test-2"
nic_list.#: <computed>
num_sockets: "1"
num_vcpus_per_socket: "2"
num_vnuma_nodes: <computed>
nutanix_guest_tools.%: <computed>
owner_reference.%: <computed>
parent_reference.%: <computed>
power_state: <computed>
power_state_mechanism: <computed>
project_reference.%: <computed>
should_fail_on_script_failure: <computed>
state: <computed>
vga_console_enabled: <computed>
+ nutanix_virtual_machine.ayan-test[2] <----------
id: <computed>
api_version: <computed>
availability_zone_reference.%: <computed>
boot_device_disk_address.%: <computed>
boot_device_mac_address: <computed>
boot_device_order_list.#: <computed>
categories.%: <computed>
cluster_name: <computed>
cluster_reference.%: <computed>
description: "ayan terra"
enable_script_exec: <computed>
gpu_list.#: <computed>
guest_customization_cloud_init_custom_key_values.%: <computed>
guest_customization_cloud_init_meta_data: <computed>
guest_customization_cloud_init_user_data: <computed>
guest_customization_is_overridable: <computed>
guest_customization_sysprep.%: <computed>
guest_customization_sysprep_custom_key_values.%: <computed>
guest_os_id: <computed>
hardware_clock_timezone: <computed>
host_reference.%: <computed>
hypervisor_type: <computed>
memory_size_mib: "4096"
metadata.%: <computed>
name: "ayan-test-3" <<---------------------
nic_list.#: <computed>
num_sockets: "1"
num_vcpus_per_socket: "2"
num_vnuma_nodes: <computed>
nutanix_guest_tools.%: <computed>
owner_reference.%: <computed>
parent_reference.%: <computed>
power_state: <computed>
power_state_mechanism: <computed>
project_reference.%: <computed>
should_fail_on_script_failure: <computed>
state: <computed>
vga_console_enabled: <computed>
+ nutanix_virtual_machine.ayan-test[3]
id: <computed>
api_version: <computed>
availability_zone_reference.%: <computed>
boot_device_disk_address.%: <computed>
boot_device_mac_address: <computed>
boot_device_order_list.#: <computed>
categories.%: <computed>
cluster_name: <computed>
cluster_reference.%: <computed>
description: "ayan terra"
enable_script_exec: <computed>
gpu_list.#: <computed>
guest_customization_cloud_init_custom_key_values.%: <computed>
guest_customization_cloud_init_meta_data: <computed>
guest_customization_cloud_init_user_data: <computed>
guest_customization_is_overridable: <computed>
guest_customization_sysprep.%: <computed>
guest_customization_sysprep_custom_key_values.%: <computed>
guest_os_id: <computed>
hardware_clock_timezone: <computed>
host_reference.%: <computed>
hypervisor_type: <computed>
memory_size_mib: "4096"
metadata.%: <computed>
name: "ayan-test-4" <---------------------
nic_list.#: <computed>
num_sockets: "1"
num_vcpus_per_socket: "2"
num_vnuma_nodes: <computed>
nutanix_guest_tools.%: <computed>
owner_reference.%: <computed>
parent_reference.%: <computed>
power_state: <computed>
power_state_mechanism: <computed>
project_reference.%: <computed>
should_fail_on_script_failure: <computed>
state: <computed>
vga_console_enabled: <computed>
+ nutanix_virtual_machine.ayan-test[4]
id: <computed>
api_version: <computed>
availability_zone_reference.%: <computed>
boot_device_disk_address.%: <computed>
boot_device_mac_address: <computed>
boot_device_order_list.#: <computed>
categories.%: <computed>
cluster_name: <computed>
cluster_reference.%: <computed>
description: "ayan terra"
enable_script_exec: <computed>
gpu_list.#: <computed>
guest_customization_cloud_init_custom_key_values.%: <computed>
guest_customization_cloud_init_meta_data: <computed>
guest_customization_cloud_init_user_data: <computed>
guest_customization_is_overridable: <computed>
guest_customization_sysprep.%: <computed>
guest_customization_sysprep_custom_key_values.%: <computed>
guest_os_id: <computed>
hardware_clock_timezone: <computed>
host_reference.%: <computed>
hypervisor_type: <computed>
memory_size_mib: "4096"
metadata.%: <computed>
name: "ayan-test-5" <<------------------------
nic_list.#: <computed>
num_sockets: "1"
num_vcpus_per_socket: "2"
num_vnuma_nodes: <computed>
nutanix_guest_tools.%: <computed>
owner_reference.%: <computed>
parent_reference.%: <computed>
power_state: <computed>
power_state_mechanism: <computed>
project_reference.%: <computed>
should_fail_on_script_failure: <computed>
state: <computed>
vga_console_enabled: <computed>
Plan: 4 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
nutanix_virtual_machine.ayan-test[3]: Creating...
api_version: "" => "<computed>"
availability_zone_reference.%: "" => "<computed>"
boot_device_disk_address.%: "" => "<computed>"
boot_device_mac_address: "" => "<computed>"
boot_device_order_list.#: "" => "<computed>"
categories.%: "" => "<computed>"
cluster_name: "" => "<computed>"
cluster_reference.%: "" => "<computed>"
description: "" => "ayan terra"
enable_script_exec: "" => "<computed>"
gpu_list.#: "" => "<computed>"
guest_customization_cloud_init_custom_key_values.%: "" => "<computed>"
guest_customization_cloud_init_meta_data: "" => "<computed>"
guest_customization_cloud_init_user_data: "" => "<computed>"
guest_customization_is_overridable: "" => "<computed>"
guest_customization_sysprep.%: "" => "<computed>"
guest_customization_sysprep_custom_key_values.%: "" => "<computed>"
guest_os_id: "" => "<computed>"
hardware_clock_timezone: "" => "<computed>"
host_reference.%: "" => "<computed>"
hypervisor_type: "" => "<computed>"
memory_size_mib: "" => "4096"
metadata.%: "" => "<computed>"
name: "" => "ayan-test-4"
nic_list.#: "" => "<computed>"
num_sockets: "" => "1"
num_vcpus_per_socket: "" => "2"
num_vnuma_nodes: "" => "<computed>"
nutanix_guest_tools.%: "" => "<computed>"
owner_reference.%: "" => "<computed>"
parent_reference.%: "" => "<computed>"
power_state: "" => "<computed>"
power_state_mechanism: "" => "<computed>"
project_reference.%: "" => "<computed>"
should_fail_on_script_failure: "" => "<computed>"
state: "" => "<computed>"
vga_console_enabled: "" => "<computed>"
nutanix_virtual_machine.ayan-test[2]: Creating...
api_version: "" => "<computed>"
availability_zone_reference.%: "" => "<computed>"
boot_device_disk_address.%: "" => "<computed>"
boot_device_mac_address: "" => "<computed>"
boot_device_order_list.#: "" => "<computed>"
categories.%: "" => "<computed>"
cluster_name: "" => "<computed>"
cluster_reference.%: "" => "<computed>"
description: "" => "ayan terra"
enable_script_exec: "" => "<computed>"
gpu_list.#: "" => "<computed>"
guest_customization_cloud_init_custom_key_values.%: "" => "<computed>"
guest_customization_cloud_init_meta_data: "" => "<computed>"
guest_customization_cloud_init_user_data: "" => "<computed>"
guest_customization_is_overridable: "" => "<computed>"
guest_customization_sysprep.%: "" => "<computed>"
guest_customization_sysprep_custom_key_values.%: "" => "<computed>"
guest_os_id: "" => "<computed>"
hardware_clock_timezone: "" => "<computed>"
host_reference.%: "" => "<computed>"
hypervisor_type: "" => "<computed>"
memory_size_mib: "" => "4096"
metadata.%: "" => "<computed>"
name: "" => "ayan-test-3"
nic_list.#: "" => "<computed>"
num_sockets: "" => "1"
num_vcpus_per_socket: "" => "2"
num_vnuma_nodes: "" => "<computed>"
nutanix_guest_tools.%: "" => "<computed>"
owner_reference.%: "" => "<computed>"
parent_reference.%: "" => "<computed>"
power_state: "" => "<computed>"
power_state_mechanism: "" => "<computed>"
project_reference.%: "" => "<computed>"
should_fail_on_script_failure: "" => "<computed>"
state: "" => "<computed>"
vga_console_enabled: "" => "<computed>"
nutanix_virtual_machine.ayan-test[4]: Creating...
api_version: "" => "<computed>"
availability_zone_reference.%: "" => "<computed>"
boot_device_disk_address.%: "" => "<computed>"
boot_device_mac_address: "" => "<computed>"
boot_device_order_list.#: "" => "<computed>"
categories.%: "" => "<computed>"
cluster_name: "" => "<computed>"
cluster_reference.%: "" => "<computed>"
description: "" => "ayan terra"
enable_script_exec: "" => "<computed>"
gpu_list.#: "" => "<computed>"
guest_customization_cloud_init_custom_key_values.%: "" => "<computed>"
guest_customization_cloud_init_meta_data: "" => "<computed>"
guest_customization_cloud_init_user_data: "" => "<computed>"
guest_customization_is_overridable: "" => "<computed>"
guest_customization_sysprep.%: "" => "<computed>"
guest_customization_sysprep_custom_key_values.%: "" => "<computed>"
guest_os_id: "" => "<computed>"
hardware_clock_timezone: "" => "<computed>"
host_reference.%: "" => "<computed>"
hypervisor_type: "" => "<computed>"
memory_size_mib: "" => "4096"
metadata.%: "" => "<computed>"
name: "" => "ayan-test-5"
nic_list.#: "" => "<computed>"
num_sockets: "" => "1"
num_vcpus_per_socket: "" => "2"
num_vnuma_nodes: "" => "<computed>"
nutanix_guest_tools.%: "" => "<computed>"
owner_reference.%: "" => "<computed>"
parent_reference.%: "" => "<computed>"
power_state: "" => "<computed>"
power_state_mechanism: "" => "<computed>"
project_reference.%: "" => "<computed>"
should_fail_on_script_failure: "" => "<computed>"
state: "" => "<computed>"
vga_console_enabled: "" => "<computed>"
nutanix_virtual_machine.ayan-test[1]: Creating...
api_version: "" => "<computed>"
availability_zone_reference.%: "" => "<computed>"
boot_device_disk_address.%: "" => "<computed>"
boot_device_mac_address: "" => "<computed>"
boot_device_order_list.#: "" => "<computed>"
categories.%: "" => "<computed>"
cluster_name: "" => "<computed>"
cluster_reference.%: "" => "<computed>"
description: "" => "ayan terra"
enable_script_exec: "" => "<computed>"
gpu_list.#: "" => "<computed>"
guest_customization_cloud_init_custom_key_values.%: "" => "<computed>"
guest_customization_cloud_init_meta_data: "" => "<computed>"
guest_customization_cloud_init_user_data: "" => "<computed>"
guest_customization_is_overridable: "" => "<computed>"
guest_customization_sysprep.%: "" => "<computed>"
guest_customization_sysprep_custom_key_values.%: "" => "<computed>"
guest_os_id: "" => "<computed>"
hardware_clock_timezone: "" => "<computed>"
host_reference.%: "" => "<computed>"
hypervisor_type: "" => "<computed>"
memory_size_mib: "" => "4096"
metadata.%: "" => "<computed>"
name: "" => "ayan-test-2"
nic_list.#: "" => "<computed>"
num_sockets: "" => "1"
num_vcpus_per_socket: "" => "2"
num_vnuma_nodes: "" => "<computed>"
nutanix_guest_tools.%: "" => "<computed>"
owner_reference.%: "" => "<computed>"
parent_reference.%: "" => "<computed>"
power_state: "" => "<computed>"
power_state_mechanism: "" => "<computed>"
project_reference.%: "" => "<computed>"
should_fail_on_script_failure: "" => "<computed>"
state: "" => "<computed>"
vga_console_enabled: "" => "<computed>"
nutanix_virtual_machine.ayan-test.4: Still creating... (10s elapsed)
nutanix_virtual_machine.ayan-test.3: Still creating... (10s elapsed)
nutanix_virtual_machine.ayan-test.2: Still creating... (10s elapsed)
nutanix_virtual_machine.ayan-test.1: Still creating... (10s elapsed)
So we now have 5 VM’s created! With unique names we can use! Stick around for more!