feat(vmware): adding vmware support

This commit is contained in:
Philippe Caseiro 2023-07-05 09:43:06 +02:00
parent d02e96d4f1
commit 0293e76708
3 changed files with 111 additions and 0 deletions

92
modules/vmware/main.tf Normal file
View File

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

11
modules/vmware/output.tf Normal file
View File

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

View File

@ -0,0 +1,8 @@
terraform {
required_providers {
vsphere = {
source = "hashicorp/vsphere"
version = "~> 2.4.0"
}
}
}