hashicorp / terraform-provider-vsphere

Terraform Provider for VMware vSphere
https://registry.terraform.io/providers/hashicorp/vsphere/
Mozilla Public License 2.0
608 stars 448 forks source link

Using modules with anti_affinity_rule #807

Closed cathode911 closed 5 years ago

cathode911 commented 5 years ago

Hello colleagues, i am struggling with finding a way to use modular approach ( module "givenname" {} ) along with resource "vsphere_compute_cluster_vm_anti_affinity_rule".

Anti-affinity resource requires a list of virtual_machine_ids to be able to create a DRS rule, although i am unable to find a way how to form that list if using terraform modules, as VM resource made from module looks like module.givenname.vsphere_virtual_machine.vm[0] (1,2, etc)

Something like virtual_machine_ids = ["${module.consul-cluster-testing.vsphere_virtual_machine.vm[*].id}"] makes no sense to terraform parser.

Any advice here?

Terraform Version

terraform -v Terraform v0.11.13

vSphere Provider Version

. ├── provider.vsphere ├── module.consul-cluster-testing │   └── provider.vsphere (inherited) └── module.saltmaster-tst └── provider.vsphere (inherited)

Affected Resource(s)

Terraform Configuration Files

// Application resource pool
resource "vsphere_resource_pool" "app_pool" {
  name                    = "Consul-Vault"
  parent_resource_pool_id = "${data.vsphere_compute_cluster.cluster1.resource_pool_id}"
}

// DRS anti-affinity rules
resource "vsphere_compute_cluster_vm_anti_affinity_rule" "consul_vms_anti_affinity_rule" {
  name                    = "consul-vms-anti-affinity-rule"
  compute_cluster_id      = "${data.vsphere_compute_cluster.cluster1.id}"
  virtual_machine_ids     = ["${module.consul-cluster-testing.vsphere_virtual_machine.vm[*].id}"]
}

Expected Behavior

Rule creation

Actual Behavior

Various errors due to inability to find dependent VM ids from module and / or parse errors

Steps to Reproduce

  1. Use module to provision VM resource
  2. Use resource "vsphere_compute_cluster_vm_anti_affinity_rule" to create anti-affinity rule
bill-rich commented 5 years ago

Hey @cathode911! You're very close on the correct syntax for what you're going for. The issue you're running into is that you're running Terraform 0.11.x and your config syntax is a combination of HCL (Terraform <0.12) and HCL2 (Terraform >=0.12).

If you'd like to stick with 0.11.x, try changing the virtual_machine_ids to:

virtual_machine_ids     = ["${module.consul-cluster-testing.vsphere_virtual_machine.vm.*.id}"]

Or you can upgrade to 0.12 and use:

virtual_machine_ids     = module.consul-cluster-testing.vsphere_virtual_machine.vm[*].id