gpt4 book ai didi

list - Terraform 合并 map 列表

转载 作者:行者123 更新时间:2023-12-04 07:44:10 25 4
gpt4 key购买 nike

我的输入如下:

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

我正在使用合并如下:
output "map_groups" {  
value = merge(var.list_groups...)
}

我得到的输出是:
map_groups = {
dev-api = {
envs = [
"dev-us-1",
"dev-us-2",
]
hosts = [
"dev-api-us1",
"dev-api-us2",
]
}
}

但我需要输出为:
map_groups = {
dev-api = {
envs = [
"dev-us-1",
"dev-us-2",
"dev-eu-1",
"dev-eu-2"
]
hosts = [
"dev-api-us1",
"dev-api-us2",
"dev-api-eu1",
"dev-api-eu2"
]
}
}

我知道合并正在吃掉其中一个条目,因为键是相同的,但如果它可以以某种方式合并条目/元素,那就太好了。

最佳答案

这是一个比较复杂的问题。我认为您应该专注于简化您的输入数据,而不是尝试提出一些复杂的 TF 代码来对您的输入数据结构进行后修复。尽管如此,TF代码是:

variable "list_groups" {

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

}


locals {
api_names = distinct([for api in var.list_groups: keys(api)[0]])
}



output "test" {
value = {for key, val in {for api_name in local.api_names:
api_name => {
envs = flatten([
for api in var.list_groups:
api[api_name].envs
])
hosts = flatten([
for api in var.list_groups:
api[api_name].hosts
])
}...
}: key => val[0]}
}
输出是:
test = {
"dev-api" = {
"envs" = [
"dev-eu-1",
"dev-eu-2",
"dev-us-1",
"dev-us-2",
]
"hosts" = [
"dev-api-eu1",
"dev-api-eu2",
"dev-api-us1",
"dev-api-us2",
]
}
}

关于list - Terraform 合并 map 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67282480/

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