过滤变量map+list



我有一个数据结构,我需要从基于maps键的列表映射中提取列表。下面是示例数据:

locals {
firwall_rules = ["first", "third"]   # this is the filter used on firewall_rules_lookup 
firewall_rules_lookup = {
type = map
"first" = [ { name ="rule1.1", start_ip="0.0.0.1" , end_ip = "0.0.0.2" },
{ name ="rule1.2", start_ip="0.0.0.4" , end_ip = "0.0.0.5" },
],

"second"= [ { name ="rule2.1", start_ip="0.0.1.1" , end_ip = "0.0.1.2" } ],

"third" = [ { name ="rule3.1", start_ip="0.0.3.1" , end_ip = "0.0.3.2" },
{ name ="rule3.2", start_ip="0.0.3.4" , end_ip = "0.0.3.5" },
]
}

fw_rules = flatten([
for rule_name in local.firewall_rules : {
for r in local.firewall_rules_lookup[rule_name] : {
name = r.name
start_ip = r.start_ip
end_ip = r.end_ip
}
}
])
}

预期结果:

fw_rules=
[ { name ="rule1.1", start_ip="0.0.0.1" , end_ip = "0.0.0.2" },
{ name ="rule1.2", start_ip="0.0.0.4" , end_ip = "0.0.0.5" },
{ name ="rule3.1", start_ip="0.0.3.1" , end_ip = "0.0.3.2" },
{ name ="rule3.2", start_ip="0.0.3.4" , end_ip = "0.0.3.5" }
]

内部for循环不起作用。Terraform给我一个错误。我认为是for循环只使用地图。这个问题有别的解决办法吗?

应该如下所示:

locals {
firewall_rules = ["first", "third"]   # this is the filter used on firewall_rules_lookup 
firewall_rules_lookup = {
"first" = [ { name ="rule1.1", start_ip="0.0.0.1" , end_ip = "0.0.0.2" },
{ name ="rule1.2", start_ip="0.0.0.4" , end_ip = "0.0.0.5" },
],

"second"= [ { name ="rule2.1", start_ip="0.0.1.1" , end_ip = "0.0.1.2" } ],

"third" = [ { name ="rule3.1", start_ip="0.0.3.1" , end_ip = "0.0.3.2" },
{ name ="rule3.2", start_ip="0.0.3.4" , end_ip = "0.0.3.5" },
]
}

fw_rules = flatten([
for rule_name in local.firewall_rules : [
for r in local.firewall_rules_lookup[rule_name] : {
name = r.name
start_ip = r.start_ip
end_ip = r.end_ip
}
]
])
}

最新更新