I currently am using a json file to iterate over list for resource creation using a for expression. A solution was provided in Iterating using templatefile() in JSON · Issue #33187 · hashicorp/terraform · GitHub. As this project has progressed I have pivoted to using yaml syntax instead for better readability and to be in-line with yaml files that we are ingesting from other sources. Is iteration possible with a yaml template file? Receiving “│ Invalid value for “vars” parameter: vars map does not contain key “index”…” error when attempting to loop through syslog servers.
resource "bigip_do" "ltm__tmpl_do" {
timeout = 15
do_json = templatefile("../templates/ltm_lab_DO.yaml.tf", {
HOSTNAME = "ltm1-lab"
NAME_SERVERS = ["8.8.8.8", "8.8.4.4"]
SYSLOG_SERVERS = ["1.1.1.1", "2.2.2.2"]
INT_VIPS_1_SELFIP_IP = "10.169.21.10/24"
INT_VIPS_2_SELFIP_IP = "10.169.22.10/24"
INT_VIPS_3_SELFIP_IP = "10.169.23.10/24"
EXT_VIPS_1_SELFIP_IP = "10.169.24.10/24"
EXT_VIPS_2_SELFIP_IP = "10.169.25.10/24"
EXT_VIPS_3_SELFIP_IP = "10.169.26.10/24"
TRAFFIC_SELFIP_IP = "10.169.27.70/27"
REMOTE_ACCESS_ALLOW_LIST = ["1.1.1.1/24", "2.2.2.2/24"]
SNMP_ALLOW_LIST = [
"127.0.0.0/8",
"10.169.117.216",
"1.1.1.1/24",
"2.2.2.2/24"
]
VPC_CIDR_BLOCK = ["10.169.21.0/19", "10.179.32.0/20"]
})
}
schemaVersion: 1.37.0
class: Device
async: true
Common:
class: Tenant
System:
class: System
hostname: ${HOSTNAME}
mgmtDhcpEnabled: false
autoCheck: false
autoPhonehome: true
Provision:
class: Provision
ltm: dedicated
NTP:
class: NTP
servers: ${NAME_SERVERS}
DNS:
class: DNS
nameServers: ${NAME_SERVERS}
search:
- ops.sfdc.net
EXT-VIPS-1-VLAN:
class: VLAN
tag: 102
interfaces:
- name: "1.2"
tagged: false
EXT-VIPS-2-VLAN:
class: VLAN
tag: 103
interfaces:
- name: "1.3"
tagged: false
EXT-VIPS-3-VLAN:
class: VLAN
tag: 104
interfaces:
- name: "1.4"
tagged: false
INT-VIPS-1-VLAN:
class: VLAN
tag: 105
interfaces:
- name: "1.5"
tagged: false
INT-VIPS-2-VLAN:
class: VLAN
tag: 106
interfaces:
- name: "1.6"
tagged: false
INT-VIPS-3-VLAN:
class: VLAN
tag: 107
interfaces:
- name: "1.7"
tagged: false
TRAFFIC-VLAN:
class: VLAN
tag: 101
interfaces:
- name: "1.1"
tagged: false
INT-VIPS-1-SELFIP:
class: SelfIp
address: INT_VIPS_1_SELFIP_IP
vlan: INT-VIPS-1-VLAN
trafficGroup: traffic-group-local-only
allowService: none
INT-VIPS-2-SELFIP:
class: SelfIp
address: INT_VIPS_2_SELFIP_IP
vlan: INT-VIPS-2-VLAN
trafficGroup: traffic-group-local-only
allowService: none
INT-VIPS-3-SELFIP:
class: SelfIp
address: INT_VIPS_3_SELFIP_IP
vlan: INT-VIPS-3-VLAN
trafficGroup: traffic-group-local-only
allowService: none
EXT-VIPS-1-SELFIP:
class: SelfIp
address: EXT_VIPS_1_SELFIP_IP
vlan: EXT-VIPS-1-VLAN
trafficGroup: traffic-group-local-only
allowService: none
EXT-VIPS-2-SELFIP:
class: SelfIp
address: EXT_VIPS_2_SELFIP_IP
vlan: EXT-VIPS-2-VLAN
trafficGroup: traffic-group-local-only
allowService: none
EXT-VIPS-3-SELFIP:
class: SelfIp
address: EXT_VIPS_3_SELFIP_IP
vlan: EXT-VIPS-3-VLAN
trafficGroup: traffic-group-local-only
allowService: none
TRAFFIC-SELFIP:
class: SelfIp
address: TRAFFIC_SELFIP_IP
vlan: TRAFFIC-VLAN
trafficGroup: traffic-group-local-only
allowService: default
Snmp:
class: SnmpAgent
allowList: ${SNMP_ALLOW_LIST}
SSHD:
class: SSHD
allow: ${REMOTE_ACCESS_ALLOW_LIST}
HTTPD:
class: HTTPD
allow: ${REMOTE_ACCESS_ALLOW_LIST}
{for index, syslog_server in SYSLOG_SERVERS : "remotesyslog${index + 1}" => {"host": syslog_server, "class": "SyslogRemoteServer"}}
Error:
│ Error: Invalid function argument
│
│ on ltm_lab.tf line 40, in locals:
│ 40: do_yaml = templatefile("../templates/ltm_lab_DO.yaml.tf", {
│ 41: HOSTNAME = "ltm1-lab"
│ 42: NAME_SERVERS = ["8.8.8.8", "8.8.4.4"]
│ 43: SYSLOG_SERVERS = ["1.1.1.1", "2.2.2.2"]
│ 44: INT_VIPS_1_SELFIP_IP = "10.169.21.10/24"
│ 45: INT_VIPS_2_SELFIP_IP = "10.169.22.10/24"
│ 46: INT_VIPS_3_SELFIP_IP = "10.169.23.10/24"
│ 47: EXT_VIPS_1_SELFIP_IP = "10.169.24.10/24"
│ 48: EXT_VIPS_2_SELFIP_IP = "10.169.25.10/24"
│ 49: EXT_VIPS_3_SELFIP_IP = "10.169.26.10/24"
│ 50: TRAFFIC_SELFIP_IP = "10.169.27.70/27"
│ 51: REMOTE_ACCESS_ALLOW_LIST = ["1.1.1.1/24", "2.2.2.2/24"]
│ 52: SNMP_ALLOW_LIST = [
│ 53: "127.0.0.0/8",
│ 54: "10.169.117.216",
│ 55: "1.1.1.1/24",
│ 56: "2.2.2.2/24"
│ 57: ]
│ 58: VPC_CIDR_BLOCK = ["10.169.21.0/19", "10.179.32.0/20"]
│ 59: })
│ ├────────────────
│ │ while calling templatefile(path, vars)
│
│ Invalid value for "vars" parameter: vars map does not contain key "index", referenced at ../templates/ltm_lab_DO.yaml.tf:116,65-70.
╵