gpt4 book ai didi

list - Terraform 遍历 map 的 map

转载 作者:行者123 更新时间:2023-12-05 06:01:13 24 4
gpt4 key购买 nike

我的输入如下:

list_groups= [
{
"dev-api-eu" = {
envs = [
"dev-eu-1",
"dev-eu-2",
]
hosts = [
"dev-api-eu1",
"dev-api-eu2",
]
}
"dev-api-uk" = {
envs = [
"dev-uk-1",
"dev-uk-2",
]
hosts = [
"dev-api-uk1",
"dev-api-uk2",
]
}
},
{
"dev-api-us" = {
envs = [
"dev-us-1",
"dev-us-2",
]
hosts = [
"dev-api-us1",
"dev-api-us2",
]
}
},
]

现在我希望我的输出是 3 个 map 和 3 个键的 map :

"dev-api-eu"
"dev-api-uk"
"dev-api-us"

值没有变化。

下面是我想要的输出:

 map_groups1      = {
dev-api-eu = {
envs = [
"dev-eu-1",
"dev-eu-2",
]
hosts = [
"dev-api-eu1",
"dev-api-eu2",
]
}
dev-api-uk = {
envs = [
"dev-uk-1",
"dev-uk-2",
]
hosts = [
"dev-api-uk1",
"dev-api-uk2",
]
}
dev-api-us = {
envs = [
"dev-us-1",
"dev-us-2",
]
hosts = [
"dev-api-us1",
"dev-api-us2",
]
}
}

如果列表中的每个项目只有一个 map ,则下面有效

map_groups1 = {
for record in local.list_groups:
keys(record)[0] => values(record)[0]
}

上面的代码跳过了键“dev-api-uk”,只在结果中给出了两个 map 。但是,如您所见,我在输入列表中的第一个项目有两个 map ,并且可能有 n 个 map 。有什么想法吗?

最佳答案

你可以只使用mergeexpantion :

output "test" {  
value = merge(var.list_groups...)
}

关于list - Terraform 遍历 map 的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67274020/

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