Terraform - foreach 地图列表中的地图列表
Terraform - foreach list of maps within a list of maps
我目前正在努力思考如何在地图列表中 foreach 地图列表。
locals {
vpn_configurations = [
{
customer_name = "test125231"
custom_path = "test123123"
shared_by = []
nat = false
nat_source_ip = ""
nat_destination_ip = ""
nat_route = ""
tunnels = [
{
tunnel_name = "test-tunnel"
left = "%defaultroute"
leftid = ""
leftsubnet = ""
leftsourceip = ""
rightid = ""
right = ""
rightsubnet = ""
rightsourceip = ""
ike = "aes256-sha256-modp2048"
keyexchange = "ike"
ikev2 = "no"
esp = "aes256-sha256-modp2048"
salifetime = 3600
ikelifetime = 3600
authby = "secret"
# use auto=start when done testing the tunnel
auto = "ondemand"
},
{
tunnel_name = "test-tunnel2"
left = "%defaultroute"
leftid = ""
leftsubnet = ""
leftsourceip = ""
rightid = ""
right = ""
rightsubnet = ""
rightsourceip = ""
ike = "aes256-sha256-modp2048"
keyexchange = "ike"
ikev2 = "no"
esp = "aes256-sha256-modp2048"
salifetime = 3600
ikelifetime = 3600
authby = "secret"
# use auto=start when done testing the tunnel
auto = "ondemand"
}
]
},
{
customer_name = "sdfsdfsd"
custom_path = "sdfsdfsdf"
shared_by = []
nat = false
nat_source_ip = ""
nat_destination_ip = ""
nat_route = ""
tunnels = [
{
tunnel_name = "test-tunnel3"
left = "%defaultroute"
leftid = ""
leftsubnet = ""
leftsourceip = ""
rightid = ""
right = ""
rightsubnet = ""
rightsourceip = ""
ike = "aes256-sha256-modp2048"
keyexchange = "ike"
ikev2 = "no"
esp = "aes256-sha256-modp2048"
salifetime = 3600
ikelifetime = 3600
authby = "secret"
# use auto=start when done testing the tunnel
auto = "ondemand"
},
{
tunnel_name = "test-tunnel4"
left = "%defaultroute"
leftid = ""
leftsubnet = ""
leftsourceip = ""
rightid = ""
right = ""
rightsubnet = ""
rightsourceip = ""
ike = "aes256-sha256-modp2048"
keyexchange = "ike"
ikev2 = "no"
esp = "aes256-sha256-modp2048"
salifetime = 3600
ikelifetime = 3600
authby = "secret"
# use auto=start when done testing the tunnel
auto = "ondemand"
}
]
}
]
}
我正在尝试将每个 vpn 配置的每个隧道传递到 terraform 模板生成器中,以便为与客户关联的每个隧道创建单独的配置文件。
data "template_file" "networking_configs" {
for_each = local.vpn_configurations
template = file("${path.module}/template-files/networking-templates/tunnel-configuration.tpl")
vars = {
tunnel_name = each.value.tunnels["tunnel_name"]
left = each.value.tunnels["left"]
leftid = module.ipsec.public_ip
leftsubnet = each.value.tunnels["leftsubnet"]
leftsourceip = data.aws_network_interface.eni_ip.private_ip
rightid = each.value.tunnels["rightid"]
right = each.value.tunnels["right"]
rightsubnet = each.value.tunnels["rightsubnet"]
rightsourceip = each.value.tunnels["rightsourceip"]
ike = each.value.tunnels["ike"]
keyexchange = each.value.tunnels["keyexchange"]
ikev2 = each.value.tunnels["ikev2"]
esp = each.value.tunnels["esp"]
salifetime = each.value.tunnels["salifetime"]
ikelifetime = each.value.tunnels["ikelifetime"]
authby = each.value.tunnels["authby"]
auto = each.value.tunnels["auto"]
}
}
感谢任何可用的帮助。
您必须将双 for 循环压缩为一个。例如(只列出了一些变量):
locals {
flat_vpn_configurations = merge([
for vpn_config in local.vpn_configurations :
{
for tunnel in vpn_config["tunnels"]:
"${vpn_config["customer_name"]}-${tunnel["tunnel_name"]}" =>
{
customer_name = vpn_config["customer_name"]
custom_path = vpn_config["custom_path"]
tunnel_name = tunnel["tunnel_name"]
left = tunnel["left"]
}
}
]...)
}
这会给你 local.flat_vpn_configurations
作为(只显示部分):
{
"sdfsdfsd-test-tunnel3" = {
"custom_path" = "sdfsdfsdf"
"customer_name" = "sdfsdfsd"
"left" = "%defaultroute"
"tunnel_name" = "test-tunnel3"
}
"sdfsdfsd-test-tunnel4" = {
"custom_path" = "sdfsdfsdf"
"customer_name" = "sdfsdfsd"
"left" = "%defaultroute"
"tunnel_name" = "test-tunnel4"
}
"test125231-test-tunnel" = {
"custom_path" = "test123123"
"customer_name" = "test125231"
"left" = "%defaultroute"
"tunnel_name" = "test-tunnel"
}
"test125231-test-tunnel2" = {
"custom_path" = "test123123"
"customer_name" = "test125231"
"left" = "%defaultroute"
"tunnel_name" = "test-tunnel2"
}
}
然后你可以很容易地用for_each
迭代flat_vpn_configurations
。
我目前正在努力思考如何在地图列表中 foreach 地图列表。
locals {
vpn_configurations = [
{
customer_name = "test125231"
custom_path = "test123123"
shared_by = []
nat = false
nat_source_ip = ""
nat_destination_ip = ""
nat_route = ""
tunnels = [
{
tunnel_name = "test-tunnel"
left = "%defaultroute"
leftid = ""
leftsubnet = ""
leftsourceip = ""
rightid = ""
right = ""
rightsubnet = ""
rightsourceip = ""
ike = "aes256-sha256-modp2048"
keyexchange = "ike"
ikev2 = "no"
esp = "aes256-sha256-modp2048"
salifetime = 3600
ikelifetime = 3600
authby = "secret"
# use auto=start when done testing the tunnel
auto = "ondemand"
},
{
tunnel_name = "test-tunnel2"
left = "%defaultroute"
leftid = ""
leftsubnet = ""
leftsourceip = ""
rightid = ""
right = ""
rightsubnet = ""
rightsourceip = ""
ike = "aes256-sha256-modp2048"
keyexchange = "ike"
ikev2 = "no"
esp = "aes256-sha256-modp2048"
salifetime = 3600
ikelifetime = 3600
authby = "secret"
# use auto=start when done testing the tunnel
auto = "ondemand"
}
]
},
{
customer_name = "sdfsdfsd"
custom_path = "sdfsdfsdf"
shared_by = []
nat = false
nat_source_ip = ""
nat_destination_ip = ""
nat_route = ""
tunnels = [
{
tunnel_name = "test-tunnel3"
left = "%defaultroute"
leftid = ""
leftsubnet = ""
leftsourceip = ""
rightid = ""
right = ""
rightsubnet = ""
rightsourceip = ""
ike = "aes256-sha256-modp2048"
keyexchange = "ike"
ikev2 = "no"
esp = "aes256-sha256-modp2048"
salifetime = 3600
ikelifetime = 3600
authby = "secret"
# use auto=start when done testing the tunnel
auto = "ondemand"
},
{
tunnel_name = "test-tunnel4"
left = "%defaultroute"
leftid = ""
leftsubnet = ""
leftsourceip = ""
rightid = ""
right = ""
rightsubnet = ""
rightsourceip = ""
ike = "aes256-sha256-modp2048"
keyexchange = "ike"
ikev2 = "no"
esp = "aes256-sha256-modp2048"
salifetime = 3600
ikelifetime = 3600
authby = "secret"
# use auto=start when done testing the tunnel
auto = "ondemand"
}
]
}
]
}
我正在尝试将每个 vpn 配置的每个隧道传递到 terraform 模板生成器中,以便为与客户关联的每个隧道创建单独的配置文件。
data "template_file" "networking_configs" {
for_each = local.vpn_configurations
template = file("${path.module}/template-files/networking-templates/tunnel-configuration.tpl")
vars = {
tunnel_name = each.value.tunnels["tunnel_name"]
left = each.value.tunnels["left"]
leftid = module.ipsec.public_ip
leftsubnet = each.value.tunnels["leftsubnet"]
leftsourceip = data.aws_network_interface.eni_ip.private_ip
rightid = each.value.tunnels["rightid"]
right = each.value.tunnels["right"]
rightsubnet = each.value.tunnels["rightsubnet"]
rightsourceip = each.value.tunnels["rightsourceip"]
ike = each.value.tunnels["ike"]
keyexchange = each.value.tunnels["keyexchange"]
ikev2 = each.value.tunnels["ikev2"]
esp = each.value.tunnels["esp"]
salifetime = each.value.tunnels["salifetime"]
ikelifetime = each.value.tunnels["ikelifetime"]
authby = each.value.tunnels["authby"]
auto = each.value.tunnels["auto"]
}
}
感谢任何可用的帮助。
您必须将双 for 循环压缩为一个。例如(只列出了一些变量):
locals {
flat_vpn_configurations = merge([
for vpn_config in local.vpn_configurations :
{
for tunnel in vpn_config["tunnels"]:
"${vpn_config["customer_name"]}-${tunnel["tunnel_name"]}" =>
{
customer_name = vpn_config["customer_name"]
custom_path = vpn_config["custom_path"]
tunnel_name = tunnel["tunnel_name"]
left = tunnel["left"]
}
}
]...)
}
这会给你 local.flat_vpn_configurations
作为(只显示部分):
{
"sdfsdfsd-test-tunnel3" = {
"custom_path" = "sdfsdfsdf"
"customer_name" = "sdfsdfsd"
"left" = "%defaultroute"
"tunnel_name" = "test-tunnel3"
}
"sdfsdfsd-test-tunnel4" = {
"custom_path" = "sdfsdfsdf"
"customer_name" = "sdfsdfsd"
"left" = "%defaultroute"
"tunnel_name" = "test-tunnel4"
}
"test125231-test-tunnel" = {
"custom_path" = "test123123"
"customer_name" = "test125231"
"left" = "%defaultroute"
"tunnel_name" = "test-tunnel"
}
"test125231-test-tunnel2" = {
"custom_path" = "test123123"
"customer_name" = "test125231"
"left" = "%defaultroute"
"tunnel_name" = "test-tunnel2"
}
}
然后你可以很容易地用for_each
迭代flat_vpn_configurations
。