terracadoles/modules/vmware/main.tf

81 lines
1.8 KiB
HCL

variable "vms" {
type = map(object(
{
domain = string
cpu = number
ram = number
guest_id = string
disks = map(object({
size = string
}))
network_interfaces = map(object(
{
name = string
mac = string
}
))
folder = string
}
))
description = "Virtual Machines description"
}
variable "cluster" {
type = object({
datacenter_name = string
datastore_name = string
resource_pool_name = string
folder = string
}
)
description = "vSphere cluster definition"
}
data "vsphere_datacenter" "datacenter" {
name = var.cluster.datacenter_name
}
data "vsphere_datastore" "datastore" {
name = var.cluster.datastore_name
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_resource_pool" "pool" {
name = var.cluster.resource_pool_name
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_network" "network" {
count = length(var.vms.network_interfaces)
name = var.networks[count.index].name
datacenter_id = data.vsphere_datacenter.datacenter.id
}
resource "vsphere_virtual_machine" "vm" {
for_each = var.vms
iterator = "machine"
name = "${machine.key}.${machine.value.domain}"
resource_pool_id = data.vsphere_resource_pool.id
datastore_id = data.vsphere_datastore.id
folder = machine.folder
num_cpus = machine.cpu
memory = machine.ram
guest_id = machine.guest_id
dynamic "network_interface" {
for_each = data.vsphere_network.network
iterator = "vnet"
content {
networkid = vnet.value.id
mac = machine.network_interfaces["${vnet.key}"].mac
}
}
dynamic "disk" {
for_each = each.key.disks
content {
label = each.key
size = each.key.size
}
}
}