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({ disks = map(object({
size = string size = string
})) }))
network_interfaces = map(object( network_interfaces = list(object(
{ {
name = string id = string
mac = string mac = string
} }
)) ))
@ -26,23 +26,14 @@ variable "cluster" {
datastore_name = string datastore_name = string
resource_pool_name = string resource_pool_name = string
folder = string folder = string
vnets = map(object({
name = string
}))
} }
) )
description = "vSphere cluster definition" 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" { data "vsphere_datacenter" "datacenter" {
name = var.cluster.datacenter_name name = var.cluster.datacenter_name
} }
@ -58,9 +49,9 @@ data "vsphere_resource_pool" "pool" {
} }
data "vsphere_network" "network" { 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 datacenter_id = data.vsphere_datacenter.datacenter.id
} }
@ -69,16 +60,18 @@ resource "vsphere_virtual_machine" "vm" {
name = "${each.key}.${each.value.domain}" name = "${each.key}.${each.value.domain}"
resource_pool_id = data.vsphere_resource_pool.pool.id resource_pool_id = data.vsphere_resource_pool.pool.id
datastore_id = data.vsphere_datastore.datastore.id datastore_id = data.vsphere_datastore.datastore.id
folder = each.value.folder folder = each.value.folder
num_cpus = each.value.cpu num_cpus = each.value.cpu
memory = each.value.ram memory = each.value.ram
guest_id = each.value.guest_id guest_id = each.value.guest_id
dynamic "network_interface" { 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 { content {
network_id = network_interface.value.id network_id = data.vsphere_network.network[network_interface.value.id].id
mac_address = local.vnets[network_interface.key].mac mac_address = network_interface.value.mac
} }
} }

View File

@ -2,10 +2,6 @@ output "shortnames" {
value = keys(var.vms) value = keys(var.vms)
} }
output "vnets" {
value = local.vnets
}
//output "virtual_machines" { //output "virtual_machines" {
// value = vsphere.virtual_machine.vm //template_nic[0].computed_mac) // value = vsphere.virtual_machine.vm //template_nic[0].computed_mac)
//} //}