terracadoles/modules/vmware/main.tf

82 lines
1.9 KiB
Terraform
Raw Normal View History

2023-07-05 09:43:06 +02:00
variable "vms" {
type = map(object(
{
domain = string
cpu = number
ram = number
guest_id = string
disks = map(object({
2023-07-05 09:43:06 +02:00
size = string
}))
network_interfaces = map(object(
{
name = string
2023-07-05 09:43:06 +02:00
mac = string
}
))
folder = string
}
))
description = "Virtual Machines description"
}
variable "cluster" {
type = map(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
2023-07-05 09:43:06 +02:00
datacenter_id = data.vsphere_datacenter.datacenter.id
}
resource "vsphere_virtual_machine" "vm" {
2023-07-05 09:43:06 +02:00
for_each = var.vms
iterator = "machine"
2023-07-05 09:43:06 +02:00
name = "${machine.key}.${machine.value.domain}"
resource_pool_id = data.vsphere_resource_pool.id
datastore_id = data.vsphere_datastore.id
2023-07-05 09:43:06 +02:00
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
2023-07-05 09:43:06 +02:00
size = each.key.size
}
}
}