I have a question around Terraform.
I have set up a few files, and I am able to create VM-s well with this on vSphere. But in the current company:
I have written a few pieces of code where, and it works well, but the problem is that each time that I want to deploy VM-s from this code to lets say clusterA, where the network is 1.2.3.4/24, gateway is 1.2.3.1, and so on, then I put these to the code as default. Then, when I to deploy to clusterB, I need to update all these variables, eg. to network being 1.2.4.4/24, gateway to 1.2.4.1 and so on. Can a switch logic be introduced somehow, so for example:
I know this is not the best approach, and since I am more well-versed in PowerShell, if Terraform does not support switch statements (or so I red), I will eventually do a workaround (writing a PS script which takes a template file, takes a switch, and depending on the user's choice replaces variables in the template file before running terraform plan / apply), but I would prefer to keep everything in Terraform...
My starter code is here (sorry for the lot of comments, it is meant to be given to more junior / not familiar with TF team members, so needs to be wordy...): https://code.prkr.li/4W4RsYTEm3
To give an example:
variable "vm_to_clone" {
description = "Enter the name of the vM that we will clone:"
# ! THIS IS THE VM THAT YOU ARE CLONING
# * changeme ->
# default = "bnw_2019_gui" # * uncomment this line for deploying in BN
default = "alw_2019_gui" # * uncomment this line for deploying in AL
}
Any tips please?
Thanks
The switch in itself is not supported by TF, but you could use regular map for what switch would be used (if I understand your use-case correctly).
So instead of heaving all your variables separate, you could bundle them into one map with keys foo and bar. Which key to use, you would provide from the console.
Rough example:
variable "cluster" {
}
variable "setup" {
default = {
"foo" = {
"vm_to_clone" = "bnw_2019_gui"
"vm_folder" = "BN-ALL-SIMPLIVITY-VMS"
# and the rest
},
"bar" = {
"vm_to_clone" = "alw_2019_gui"
"vm_folder" = "AL-ALL-SIMPLIVITY-VMS"
# and the rest
},
}
}
Then, depending on your cluster (either foo or bar), you would use different values of the veriables in the rest of your code:
data "vsphere_virtual_machine" "template_vm" {
name = var.setup[var.cluster].vm_to_clone
datacenter_id = data.vsphere_datacenter.target_datacenter.id
}
# and similarly for the rest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With