Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform switch?

Tags:

terraform

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:

  • we have two clusters where VM-s can be deployed
  • we also have two AD domains where VM-s can be joined

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:

  • if a variable (read this from console, something similar to PS-s Read-Host) is 'foo', use this set or variables. If that variable is 'bar', use a different set of variables. If the variable the user entered is neither 'foo' or 'bar', then throw an error?

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
}
  • currently this would use the template I have set up in "ALW" cluster. To use the template of "BNW" cluster, I would need to comment the 'bnw_2019_gui' line out and comment the 'alw_2019_gui' line. (Or of course give an overriding value with the terraform plan / apply). But I would prefer switching based on the chosen cluster.

Any tips please?

Thanks

like image 389
krapulax Avatar asked May 07 '26 00:05

krapulax


1 Answers

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.
like image 161
Marcin Avatar answered May 10 '26 12:05

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!