Creating Terraform Module for VMware VMs with ability to add disks dynamically

1,081 views
Skip to first unread message

Robert Tilson

unread,
Mar 7, 2018, 2:05:09 PM3/7/18
to Terraform

Creating a module for VM creation in VMware. Looking to clone a VM template.

So the hangup is when the need is to add more disks that just the single disk that the template provides, or add a disk in the future if needed

I don't know if adding another disk section in the virtual machine resource and setting the variable type to map using interpolation syntax would work?

If would be nice if there was a way to use something like jinja for templating.

Module work in progress:

data "vsphere_datacenter" "dc" {
  name = "${var.vmw_dc}"
}

data "vsphere_datastore" "datastore" {
  name          = "${var.vmw_datastore}"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_resource_pool" "pool" {
  name          = "${var.vmw_cluster}"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_network" "network" {
  name          = "${var.vmw_network}"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

resource "vsphere_virtual_machine" "vm" {
  name             = "${var.vm_name}"
  resource_pool_id = "${data.vsphere_resource_pool.pool.id}"
  datastore_id     = "${data.vsphere_datastore.datastore.id}"

  num_cpus = "${var.vm_cpu}"
  memory   = "${var.vm_mem}"
  guest_id = "${var.vm_GuestType}"

  network_interface {
    network_id = "${data.vsphere_network.network.id}"
  }

  disk {
    label = "disk0"
    size  = 20
  }
  disk {
    label = "${var.vm_disk.*.label, count.index}"
  }
}

Doing this as a non-module would be easy. Just have a bunch of resource declarations like the above code. Or create a module and just pass the variables through in module per VM needing created.

The above would work for a single disk VM if you ignore the second Disk declaration. But if I need to add the second disk a second disk declaration is needed and a third and so on.

So I want sure if creating a map or a list of map variable that contains Disk2 through X then in the second disk declaration the other disks will get rendered when executing terraform plan.

"vm_disk" = {
Disk2 = {label=X,Size=X,Position=X},
Disk3 = {label=X,Size=X,Position=X},
Disk4 = {label=X,Size=X,Position=X},
}

Charles Harvey

unread,
Aug 23, 2018, 3:13:59 PM8/23/18
to Terraform
Did you ever figure this out?  I have the exact same problem.  Dev, Test environments require less disk size than production, I want to be able to pass the number of disks and the sizes through a var-file. 

Daniel Siegenthaler

unread,
Aug 27, 2018, 4:11:06 AM8/27/18
to Terraform
A count var and foreach loop will be in the next release 12. 
You'll be able to use a foreach loop to get the values of a list out of this file.

Rich Siegel

unread,
Jun 13, 2019, 8:41:47 PM6/13/19
to Terraform
Reply all
Reply to author
Forward
0 new messages