gpt4 book ai didi

azure - Terraform:如何从列表中的 map 嵌套列表中选择值?

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

因此,在下面我有子网,它是主 vnet 列表中的映射列表。如果我要为 azure 创建子网资源,如何提取每个资源的子网名称?如果我要更深入一点,我将如何提取 SG 名称?

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来获取子网名称和安全组名称:

locals {

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

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

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

}

output "subnet_names" {
value = local.subnet_names
}

output "subnet_names_with_vnet_name" {
value = local.subnet_names_with_vnet_name
}

output "security_group_names" {
value = local.security_group_names
}

结果是:

security_group_names = [
"first-sg",
"second-sg",
]
subnet_names = [
"first-subnet",
"second-subnet",
]
subnet_names_with_vnet_name = {
"test-vnet" = [
"first-subnet",
"second-subnet",
]
}

关于azure - Terraform:如何从列表中的 map 嵌套列表中选择值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66429342/

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