Deploy multiple VMs to multiple virtual centers

Hi ,

I need help with getting terraform to deploy multiple VMs on two different vcenters. I need to get the parameters for each vcenter build based on “nested conditions” of supplied values .

As example to get VM name prefix I need to get TF code wokring with this logic:

name = If var.build_variables[“build_prefix”] exist use it , else { if var.build_variables[“dc”] == dc1 use var.default_dc1_prefix else { if var.build_variables[“dc”] == dc2 , use var.default_dc2_prefix } else use var.default_generic_prefix}

The resource pool/datacenter/datastore will use same logic.

Not sure the efficient way to approach it.

Thanks

Tam

snippet from current code

terraform variables

dc1_variables = {
vm_data_disk = “50”
folder = “abc/vms”
datastore_cluster=“dc1-ds01”
network = “10.94.16.0-dc1”
datacenter = “dc1”
vm_count = “2”
build_prefix = “dc1_vm”
}

dc2_variables = {
vm_data_disk = “10”
folder = “xyz/vms”
datastore_cluster=“dc2-ds01”
network = “10.90.18.0-dc2”
datacenter = “dc2”
vm_count = “4”
build_prefix = “dc2_vm”
}

main.tf

module “BuildVMsdc1” {
source = “.//build-vm”
providers = {
vsphere = vsphere.dc1
}
build_variables = var.dc1_variables
}

module “BuildVMsdc2” {
source = “.//build-vm”
providers = {
vsphere = vsphere.dc2
}
build_variables = var.dc2_variables
}

Hi ,

Are you able to deploy this , build VM based on the provided vcenter location as input