gpt4 book ai didi

azure - Terraform:当传入的变量是 map 列表的列表时,如何迭代资源组中的名称或另一个变量?

转载 作者:行者123 更新时间:2023-12-03 02:27:24 24 4
gpt4 key购买 nike

这就是我到目前为止所拥有的,虽然我可以使用局部变量提取特定列表或名称,但我在将其转换到资源组时遇到问题。首先,我是否以正确的方式尝试这样做?如果不是,我如何迭代子网的名称,以便添加属于相应 vnet 映射的子网?

variable "vnets" {
default = [
{
vnet_name = "test-vnet"
address_space = "10.250.0.0"
network_size = 16
subnets = [
{
name = "first-subnet"
network_security_group = "first-nsg"
security_group_rules = [
{
name = "first-sg"
priority = 100
}
]
},
{
name = "second-subnet"
network_security_group = "second-nsg"
security_group_rules = [
{
name = "second-sg"
priority = 100
}
]
}
]
}
]
}


locals {

subnet_names = {
for vnet in var.vnets[*]:
(vnet.vnet_name) => vnet.subnets[*].name
}

security_group_names = flatten(var.vnets[*].subnets[*].security_group_rules[*].name)

}

resource "azurerm_subnet" "subnets" {
count = length(var.vnets)
#??? name = locals.subnet_names[count.index].subnets.name


resource_group_name = data.azurerm_resource_group.network_group.name
virtual_network_name = azurerm_virtual_network.vnets.*.name
address_prefixes = ["10.0.1.0/24"]

}

最佳答案

我认为最简单的方法是展平您的subnet_names:

locals {

subnet_names = {
for vnet in var.vnets[*]:
(vnet.vnet_name) => vnet.subnets[*].name
}

security_group_names = flatten(var.vnets[*].subnets[*].security_group_rules[*].name)


# uniqueness of "${vnet}-${subnet}" pairs is assumed. it will not work
# if the pairs are not unique
subnet_names_flat = merge([
for vnet, subnets in local.subnet_names:
{
for subnet in subnets:
"${vnet}-${subnet}" => {name = vnet, subnet = subnet}
}
]...)

}

这将导致 subnet_names_flat 为:

{
"test-vnet-first-subnet" = {
"name" = "test-vnet"
"subnet" = "first-subnet"
}
"test-vnet-second-subnet" = {
"name" = "test-vnet"
"subnet" = "second-subnet"
}
}

然后您的 azurerm_subnet.subnets 可能如下所示。但是,我无法验证您的 azurerm_subnet 的正确性,因此您可能需要进一步更改它。但其想法是迭代 local.subnet_names_flat,这使得 for_each 非常易于使用:

resource "azurerm_subnet" "subnets" {

for_each = local.subnet_names_flat

name = each.value.subnet
resource_group_name = data.azurerm_resource_group.network_group.name
virtual_network_name = each.value.vnet
address_prefixes = ["10.0.1.0/24"]
}

关于azure - Terraform:当传入的变量是 map 列表的列表时,如何迭代资源组中的名称或另一个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66431189/

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