diff --git a/examples/linux/main.tf b/examples/linux/main.tf index f354419..6bc9428 100644 --- a/examples/linux/main.tf +++ b/examples/linux/main.tf @@ -36,6 +36,8 @@ module "example-server-linuxvm-advanced" { "VM Network" = ["192.168.0.4", ""] // Here the first instance will use Static Ip and Second set to DHCP "test" = ["", "192.168.0.3"] } + disk_datastore = "vsanDatastore" + data_disk_datastore = ["vsanDatastore", "nfsDatastore"] data_disk_size_gb = [10, 5] // Aditional Disks to be used thin_provisioned = ["true", "false"] vmdns = ["192.168.0.2", "192.168.0.1"] diff --git a/examples/windows/main.tf b/examples/windows/main.tf index 69fe001..46e8b9e 100644 --- a/examples/windows/main.tf +++ b/examples/windows/main.tf @@ -56,6 +56,8 @@ module "example-server-windowsvm-advanced" { "VM Network" = ["192.168.0.4", ""] // Here the first instance will use Static Ip and Second DHCP "test" = ["", "192.168.0.3"] } + disk_datastore = "vsanDatastore" + data_disk_datastore = ["vsanDatastore", "nfsDatastore"] data_disk_size_gb = [10, 5] // Aditional Disks to be used thin_provisioned = ["true", "false"] vmdns = ["192.168.0.2", "192.168.0.1"] diff --git a/main.tf b/main.tf index e5fedf5..4b46176 100644 --- a/main.tf +++ b/main.tf @@ -14,6 +14,18 @@ data "vsphere_datastore" "datastore" { datacenter_id = data.vsphere_datacenter.dc.id } +data "vsphere_datastore" "disk_datastore" { + count = var.disk_datastore != "" ? 1 : 0 + name = var.disk_datastore + datacenter_id = data.vsphere_datacenter.dc.id +} + +data "vsphere_datastore" "data_disk_datastore" { + for_each = toset(var.data_disk_datastore) + name = each.key + datacenter_id = data.vsphere_datacenter.dc.id +} + data "vsphere_resource_pool" "pool" { name = var.vmrp datacenter_id = data.vsphere_datacenter.dc.id @@ -95,6 +107,7 @@ resource "vsphere_virtual_machine" "Linux" { unit_number = template_disks.key thin_provisioned = data.vsphere_virtual_machine.template.disks[template_disks.key].thin_provisioned eagerly_scrub = data.vsphere_virtual_machine.template.disks[template_disks.key].eagerly_scrub + datastore_id = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null } } @@ -108,6 +121,7 @@ resource "vsphere_virtual_machine" "Linux" { unit_number = terraform_disks.key + local.template_disk_count thin_provisioned = var.thin_provisioned != null ? var.thin_provisioned[terraform_disks.key] : null eagerly_scrub = var.eagerly_scrub != null ? var.eagerly_scrub[terraform_disks.key] : null + datastore_id = length(var.data_disk_datastore) > 0 ? data.vsphere_datastore.data_disk_datastore[var.data_disk_datastore[terraform_disks.key]].id : null } } @@ -185,6 +199,7 @@ resource "vsphere_virtual_machine" "Windows" { unit_number = template_disks.key thin_provisioned = data.vsphere_virtual_machine.template.disks[template_disks.key].thin_provisioned eagerly_scrub = data.vsphere_virtual_machine.template.disks[template_disks.key].eagerly_scrub + datastore_id = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null } } @@ -198,6 +213,7 @@ resource "vsphere_virtual_machine" "Windows" { unit_number = terraform_disks.key + local.template_disk_count thin_provisioned = var.thin_provisioned != null ? var.thin_provisioned[terraform_disks.key] : null eagerly_scrub = var.eagerly_scrub != null ? var.eagerly_scrub[terraform_disks.key] : null + datastore_id = length(var.data_disk_datastore) > 0 ? data.vsphere_datastore.data_disk_datastore[var.data_disk_datastore[terraform_disks.key]].id : null } } diff --git a/variables.tf b/variables.tf index 7ba372d..da335ac 100644 --- a/variables.tf +++ b/variables.tf @@ -156,6 +156,20 @@ variable "data_disk_size_gb" { default = [] } +variable "disk_datastore"{ + type = string + default = "" +} + +variable "data_disk_datastore" { + type = list + default = [] + # validation { + # condition = length(var.disk_datastore) == 0 || length(var.disk_datastore) == length(var.data_disk_size_gb) + # error_message = "The list of disk datastore must be equal in length to disk_size_gb" + # } +} + variable "thin_provisioned" { description = "If true, this disk is thin provisioned, with space for the file being allocated on an as-needed basis." type = list