Team,
We are facing an issue with the Lookup function. It’s not picking up the default value. I’ve defined the datastore
default values in the variable.tf
as a map and passed the temporary value from the tfvars.
I’ve got two keys defined in the TFvars. One of the keys doesn’t contain the datastore
value; the lookup function should pick up the default value for that key, but it does not. Any suggestions?
Module code: (Adding the relevant codes.)
module "vm_creation" {
for_each = var.vm
source = "Terraform-VMWare-Modules/vm/vsphere"
version = "3.8.0"
dc = var.dc_name[var.vsphere_datacentre]
datastore = lookup(each.value, "datastore_name", var.datastore_name[var.vsphere_datacentre])
network = each.value.network
staticvmname = "${var.vsphere_datacentre}-${each.value.vmname}"
vmfolder = var.folder_name
vmgateway = each.value.vmgateway
vmrp = var.pool_name[var.vsphere_datacentre]
vmtemp = var.vm_template[var.vsphere_datacentre]
...
}
Variable:
variable “vm” {
type = map(object({
network = map(list(string))
vmname = string
vmgateway = string
datastore_name = string
}))
}
variable “datastore_name” {
description = “Provide the datastore name”
type = map(string)
default = {
idev = “idev_nfs_hx”
lon = “lon_nfs_hx”
rtp = “rtp_nfs_hx”
}
}
TFVARS:
vsphere_datacentre = “idev”
domain_name = “idev.com”
vsphere_env = “idev”
vm = {
coredns_vm_1 = {
vmname = “coredns-1”,
vmgateway = “10.15.125.1”
network = {
“vlan125” = [“10.15.125.101”],
}
datastore_name = “idev-011-raid1”
},
coredns_vm_2 = {
vmname = “coredns-2”,
vmgateway = “10.15.125.1”
network = {
“vlan125” = [“10.15.125.102”],
}
}
}
ERROR:
╷
│ Error: Invalid value for input variable
│
│ on tfvars/idev.tfvars line 15:
│ 15: vm = {
│ 16: coredns_vm_1 = {
│ 17: vmname = “coredns-1”,
│ 18: vmgateway = “10.15.125.1”
│ 19: network = {
│ 20: “vlan125” = [“10.15.125.101”],
│ 21: }
│ 22: datastore_name = “idev-esx-base-011-local-raid1”
│ 23: },
│ 24: coredns_vm_2 = {
│ 25: vmname = “coredns-2”,
│ 26: vmgateway = “10.15.125.1”
│ 27: network = {
│ 28: “vlan125” = [“10.15.125.102”],
│ 29: }
| 30:
│ 31: }
│ 32: }
│
│ The given value is not suitable for var.vm declared at variables.tf:115,1-14: element “coredns_vm_2”: attribute “datastore_name” is required.