Vsphere : conditional template cloning

hi everyone and thanks for your great tools !

i have the following as input variable (through command line) :

variable "virtual_machines" {
	type = map(object({
			Gateway = optional(string,"")
			VersionWindows = optional(string,"19")
			OSType = optional(string,"CORE")
			ServerType = optional(string,"GENERIC")
			IP= optional(string,"")

The key here is the ostype/VersionWindows : i need to iterate that class with for_each, and i need to use a different template for each of my vm : “WindowsServerCore22”, “WindowsServerGUI19” for instance

before implementing the for_each, i used the following code :

data "vsphere_virtual_machine" "template" {
  name          = "${local.vsphere_virtual_machine_template}"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"

locals {

So here, the challenge for me would be to dynamically change the : name = “${local.vsphere_virtual_machine_template}” instruction according to the variables in the foreach…

Do someone have an idea ?

Thanks in advance for your help !


Hello guys !

a small up here, we could sum this up as : “Can we change “Data” fields with locals variables” ? it seems i cannot use locals in data block. Is there another way? how would you do?

Thanks again everyone