diff --git a/modules/vmware/main.tf b/modules/vmware/main.tf new file mode 100644 index 0000000..b80092b --- /dev/null +++ b/modules/vmware/main.tf @@ -0,0 +1,92 @@ +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" +} + +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 +} + +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(local.vnets) + + name = local.vnets[count.index].name + datacenter_id = data.vsphere_datacenter.datacenter.id +} + +resource "vsphere_virtual_machine" "vm" { + for_each = var.vms + 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 + 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 } + content { + network_id = network_interface.value.id + mac_address = local.vnets[network_interface.key].mac + } + } + + dynamic "disk" { + for_each = each.value.disks + content { + label = disk.key + size = disk.value.size + } + } +} \ No newline at end of file diff --git a/modules/vmware/output.tf b/modules/vmware/output.tf new file mode 100644 index 0000000..739449a --- /dev/null +++ b/modules/vmware/output.tf @@ -0,0 +1,11 @@ +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 diff --git a/modules/vmware/version.tf b/modules/vmware/version.tf new file mode 100644 index 0000000..da87832 --- /dev/null +++ b/modules/vmware/version.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + vsphere = { + source = "hashicorp/vsphere" + version = "~> 2.4.0" + } + } +} \ No newline at end of file