Hi!
I have the following impediment and need:
I’m creating a provisioning VM’s using the VSphere provider, everything is working fine and the VM is being provisioned correctly.
I have a problem related to the folder in which Terraform will allocate the VM, inside VSphere.
Currently my scenario is as follows:
I have a locals doing all the logic for deciding which Datacenter we have will install the VM, basically the scenario is as follows:
terraform.tfvars
hostname = [
"webserver-01",
"webserver-01-ro",
]
env="DC1"
variables.tf
locals {
folder = var.env == "DC1" ? "Folder/Path/In/DC/1" : "Folder/Path/In/DC/2"
}
resource "vsphere_virtual_machine" "vm"
...
folder = local.folder
...
In this scheme, it is working perfectly, however, I am trying to do the following:
Note that the hostname list has a server called ‘webserver-01-ro’ and this hostname is allocated in different folders in both Datacenters, for example:
I need webserver-01 to be allocated in folder = “CompanyName/DC1/Folder/Path/In/DC/1”
I need the webserver-01-ro to be allocated in the folder=“CompanyName/DC1/Folder/Path/In/DC/1/ro”
And when the var.env is “DC2”, I need this:
I need webserver-01 to be allocated in folder = “DC2/Folder/Path/DC/2”
I need the webserver-01-ro to be allocated in the folder = “DC2/Folder/Path/DC/2/ro”
You can see that the folders are not symmetrical, correct?
I tried to make several conditions for this, but I wasn’t successful, I would need Terraform to read each attribute of the ‘hostname’ map and assign the correct value to this attribute, based on my var.env.
I’ve tried to do it the following way, in recent attempts:
folder = [
for host in var.hostname :
var.env == "DC1" && regexall("ro", host) ? "CompanyName/DC1/Folder/Path/In/DC/1/ro" :
var.env == "DC1" && regexall("ro", host) ? "CompanyName/DC1/Folder/Path/In/DC/1" :
var.env == "DC2" && regexall("ro", host) ? "DC2/Folder/Path/DC/2/ro" : "DC2/Folder/Path/DC/2"
]
Can you help me? Any tips?