josenk / terraform-provider-esxi

Terraform-provider-esxi plugin
GNU General Public License v3.0
543 stars 154 forks source link

Help with disks #192

Closed MarkLFT closed 1 year ago

MarkLFT commented 1 year ago

I have created a module that creates VM's using this provider. Most values are common across all VM's so those are kept within the module. Properties that change between instances are passed in as variables. All works well, except disks.

I am passing in a map variable that contains zero or more disk parameters, the key is the disk name, then we have size and type values.

But I am not sure how I can use this map to create the disk resources and then virtual disks in the esxi_guest resource.

As you would expect I do not want any extra disks if the map is null or empty, and one disk for each map object if any exist.

Can someone please help with suggestions on how I might achieve this.

A copy of my main.tf from the module is below.

terraform {
  required_version = ">= 0.13"
  required_providers {
    esxi = {
      source = "registry.terraform.io/josenk/esxi"
    }
  }
}

#################
# Virtual Disks
#################
resource "esxi_virtual_disk" "vdisks" {

  for_each = var.extra_disks

  virtual_disk_disk_store = var.disk_store
  virtual_disk_dir        = var.guest_name
  virtual_disk_name       = each.key
  virtual_disk_size       = each.value.size
  virtual_disk_type       = each.value.type
}

resource "esxi_guest" "Default" {
  guest_name = var.guest_name

  disk_store     = var.disk_store
  boot_disk_type = var.disk_type_1
  boot_disk_size = var.disk_size_1
  boot_firmware  = "BIOS"

  memsize  = var.memsize
  numvcpus = var.numvcpus

  guestos = var.guest_os

  ovf_source = var.ovf_source
  power      = var.power

  network_interfaces {
    virtual_network = var.virtual_network
  }

  virtual_disks {
    virtual_disk_id = esxi_virtual_disk.vdisk1.id
    slot            = "0:1"
  }

  guestinfo = {
    "userdata.encoding" = "gzip+base64"
    "userdata"          = var.cloud_init_cfg
    "metadata.encoding" = "gzip+base64"
    "metadata"          = var.cloud_init_netplan
  }
}
MarkLFT commented 1 year ago

I found the solution, needed to use dynamic for the virtual_disks in the guest resource.