I’m struggling to find out the correct way to deploy new vms on vSphere.
I’ve defined in the variables.tf list of Datastore, Host and VM name :
variable "vm_names" {
type = list(string)
default = ["vm1", "vm2", "vm3", "vm4", "vm5"] # Liste de VMs
}
variable "datastores" {
type = list(string)
default = ["datastore1", "datastore2"] # Liste de datastores existants
}
variable "hosts" {
type = list(string)
default = ["host1", "host2", "host3"] # Liste d'hôtes existants
}
My issue is that i’m unable to loop on the lenght of var.vm_names and randomnise the selection of datastore and host of each vm because i’ve more VM than I have Datastores and Hosts
You said “randomize” but I wonder if in practice it’s okay for the result to be deterministic but somewhat evenly spread.
If so, you can use the element function to fetch an element using that function’s “wrap around” behavior, where indices greater than those directly in the list will be taken modulo the list length instead of returning an error:
element(var.datastores, index)
(where “index” is whatever expression returns the current index of var.vm_names.)