我正在尝试找出如何使用Terraform 0.12.10从以下tfvars文件中给出的复杂对象中迭代嵌套变量的方法:
virtual_network_data = {
1 = {
product_instance_id = 1
location = "somewhere"
address_space = ["192.168.0.0/23"]
dns_servers = []
custom_tags = {"test":"test value"}
subnets = [
{
purpose = "mgmt"
newbits = 4
item = 0
},
{
purpose = "transit"
newbits = 4
item = 1
}
]
}
}
variable "virtual_network_data" {} #Data comes from example.tfvars
variable "resource_group_name" {
default = "my_resource_group"
}
variable "virtual_network_name" {
default = "my_virtual_network"
}
####
resource "azurerm_subnet" "pool" {
for_each = var.virtual_network_data
name = format("%s%s%02d", "subnet_", s.purpose, s.item)
resource_group_name = var.resource_group_name
virtual_network_name = var.virtual_network_name
address_prefix = cidrsubnet(each.value["address_space"], s.newbits, s.item)
}
在example.tf
中,我可以使用each.value["address_space"]
来获取顶级变量,但是我无法解决如何获取subnets
(s.purpose
,s.item
和s.newbits
)。
我已经使用了动态块作为父资源(如下)的一部分,它可以工作,但是在这种情况下,我需要将子网移动到它自己的资源中。简而言之,如何使第一个for_each
的行为像动态块中的第二个for_each
一样?
resource "azurerm_virtual_network" "pool" {
for_each = var.virtual_network_data
name = format("%s%02d", local.resource_name, each.key)
resource_group_name = var.resource_group_name
location = each.value["location"]
address_space = each.value["address_space"]
dns_servers = each.value["dns_servers"]
tags = merge(local.tags, each.value["custom_tags"])
dynamic "subnet" {
for_each = [for s in each.value["subnets"]: {
name = format("%s%s%02d", "subnet_", s.purpose, s.item)
prefix = cidrsubnet(element(each.value["address_space"],0), s.newbits, s.item)
}]
content {
name = subnet.value.name
address_prefix = subnet.value.prefix
}
}
}
厚颜无耻的奖金,有没有办法用s.item
或each.key
之类的东西来代替count.index
?
TIA
答案 0 :(得分:1)
这种情况下的技术是使用其他Terraform语言功能将您的集合转换为适合for_each
参数的形状:每个资源实例一个元素。
对于嵌套数据结构,可以将flatten
与两个或多个for
表达式结合使用,以产生一个平面数据结构,每个嵌套对象只有一个元素:
locals {
network_subnets = flatten([
for network_key, network in var.virtual_network_data : [
for subnet in network.subnets : {
network_key = network_key
purpose = subnet.purpose
parent_cidr_block = network.address_space[0]
newbits = subnet.newbits
item = subnet.item
}
]
])
}
然后,您可以使用local.network_subnets
作为重复的基础:
resource "azurerm_subnet" "pool" {
# Each instance must have a unique key, so we'll construct one
# by combining the network key, the subnet "purpose", and the "item".
for_each = {
for ns in local.network_subnets : "${ns.network_key}.${ns.purpose}${ns.item}" => ns
}
name = format("%s%s%02d", "subnet_", each.value.purpose, each.value.item)
resource_group_name = var.resource_group_name
virtual_network_name = var.virtual_network_name
address_prefix = cidrsubnet(each.value.parent_cidr_block, each.value.newbits, each.value.item)
}
在the flatten
documentation中有一个类似的示例,作为其他一些上下文。