This commit is contained in:
Philippe Caseiro 2023-07-05 15:44:18 +02:00
parent 0293e76708
commit b6a0147749
2 changed files with 13 additions and 24 deletions

View File

@ -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
}
}

View File

@ -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)
//}