diff --git a/modules/vmware/main.tf b/modules/vmware/main.tf index b80092b..52b1438 100644 --- a/modules/vmware/main.tf +++ b/modules/vmware/main.tf @@ -8,9 +8,9 @@ variable "vms" { disks = map(object({ size = string })) - network_interfaces = map(object( + network_interfaces = list(object( { - name = string + id = string mac = string } )) @@ -26,23 +26,14 @@ variable "cluster" { datastore_name = string resource_pool_name = string folder = string + vnets = map(object({ + name = string + })) } ) description = "vSphere cluster definition" } -locals { - vnets = flatten([ - for vm_key, vm in var.vms : [ - for vnet in vm.network_interfaces : { - vm_key = "${vm_key}.${vm.domain}" - name = vnet.name - mac = vnet.mac - } - ] - ]) -} - data "vsphere_datacenter" "datacenter" { name = var.cluster.datacenter_name } @@ -58,9 +49,9 @@ data "vsphere_resource_pool" "pool" { } data "vsphere_network" "network" { - count = length(local.vnets) + for_each = var.cluster.vnets - name = local.vnets[count.index].name + name = each.value.name datacenter_id = data.vsphere_datacenter.datacenter.id } @@ -69,16 +60,18 @@ resource "vsphere_virtual_machine" "vm" { name = "${each.key}.${each.value.domain}" resource_pool_id = data.vsphere_resource_pool.pool.id datastore_id = data.vsphere_datastore.datastore.id - folder = each.value.folder + folder = each.value.folder num_cpus = each.value.cpu memory = each.value.ram guest_id = each.value.guest_id dynamic "network_interface" { - for_each = { for idx, vnet in data.vsphere_network.network : idx => vnet } + for_each = each.value.network_interfaces + //for_each = { for idx, value in each.value.network_interfaces : idx => value } + //for_each = { for idx, vnet in data.vsphere_network.network : idx => vnet } content { - network_id = network_interface.value.id - mac_address = local.vnets[network_interface.key].mac + network_id = data.vsphere_network.network[network_interface.value.id].id + mac_address = network_interface.value.mac } } diff --git a/modules/vmware/output.tf b/modules/vmware/output.tf index 739449a..c368c0c 100644 --- a/modules/vmware/output.tf +++ b/modules/vmware/output.tf @@ -2,10 +2,6 @@ output "shortnames" { value = keys(var.vms) } -output "vnets" { - value = local.vnets - -} //output "virtual_machines" { // value = vsphere.virtual_machine.vm //template_nic[0].computed_mac) //} \ No newline at end of file