I am working on use case to provision vm with for_each loop iteration in vsphere target.
i would like to launch VM with disk define corresponding to vm name in variables.tf
please check the below code
variable "demo" {
type = map(object({
ip_address = string
hostname = string
subnet = string
vcpu = string
ram = string
disks = list(map(any))
}))
default = {
demo1 = {
ip_address = "1111.1111.1111.1111"
hostname = "test1"
subnet = "subnet1"
vcpu = "4"
ram = "8"
disks = [
{
name = "disk0"
size = 100
unit_number = 0
},
{
name = "disk1"
size = 300
unit_number = 1
}
]
},
demo2 = {
ip_address = "222.222.222.222"
hostname = "test2"
subnet = "subnet2"
vcpu = "4"
ram = "8"
disks = [
{
name = "disk0"
size = 100
unit_number = 0
},
{
name = "disk1"
size = 300
unit_number = 1
}
]
},
demo3 = {
ip_address = "333.333.333.333"
hostname = "test3"
subnet = "subnet3"
vcpu = "4"
ram = "8"
disks = [
{
name = "disk0"
size = 100
unit_number = 0
},
{
name = "disk1"
size = 600
unit_number = 1
}
]
}
}// close default
}// close main
############# locals.tf ################
locals {
# flatten ensures that this local value is a flat list of objects, rather
# than a list of lists of objects.
env_provisioning = flatten([
for vm_name, vm_resources in var.demo : [
for disk in vm_resources.disks : {
vm_name = vm_name
ip_address = vm_resources.ip_address
hostname = vm_resources.hostname
subnet = vm_resources.subnet
vcpu = vm_resources.vcpu
ram = vm_resources.ram
name = disk.name
size = disk.size
unit_number = disk.unit_number
//cidr_block = subnet.cidr_block
}
]
])
}
#################### vm.tf ##############
resource "vsphere_virtual_machine" "this" {
for_each = {
for disk in local.env_provisioning: "${disk.vm_name}.${disk.name}" => disk }
name = each.value.hostname
resource_pool_id = data.vsphere_resource_pool.default.id
datastore_id = data.vsphere_datastore.datastore.id
folder = data.vsphere_folder.folder.path
firmware = "efi"
num_cpus = each.value.vcpu
memory = each.value.ram
guest_id = data.vsphere_virtual_machine.template.guest_id
network_interface {
network_id = (each.value.subnet == var.backend_subnet ? data.vsphere_network.backend-subnet1.id : data.vsphere_network.frontend-subnet0.id)
}
disk {
label = each.value.name
size = each.value.size
unit_number = each.value.unit_number
}
clone {
template_uuid = data.vsphere_virtual_machine.template.id
customize {
linux_options {
host_name = each.value.hostname
domain = var.internal_domain
}
network_interface {
ipv4_address = (each.value.ip_address)
ipv4_netmask = (each.value.subnet == var.backend_subnet.name ? var.backend_subnet.cidr : var.frontend_subnet.cidr)
}
ipv4_gateway = (each.value.subnet == var.backend_subnet.name ? var.backend_subnet.gateway : var.frontend_subnet.gateway)
dns_server_list = var.dns
}
}
}
Expected Results:
after running above code its iterating only for one disk which disk0, its not adding disk1 which is mention in variables.tf for all vm’s
i want for every VM define in variables.tf number of corresponding disk should be also added