gpt4 book ai didi

terraform - 在资源级别使用 for/for_each 迭代嵌套数据

转载 作者:行者123 更新时间:2023-12-04 11:42:58 26 4
gpt4 key购买 nike

我正在尝试解决如何使用 Terraform 0.12.10 从以下 tfvars 文件中给出的复杂对象中迭代嵌套变量:

例子.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
}
]
}
}

例子.tf
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.purposes.items.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.keycount.index ?

TIA

最佳答案

这种情况下的技术是使用其他 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 中有一个类似的例子,作为一些额外的上下文。

关于terraform - 在资源级别使用 for/for_each 迭代嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com