英文:
How can I create a dynamic list in Terraform that combines a list of strings with a list of maps?
问题
合并具有另一个地图列表的列表在terraform中
我们有一个名为listA的列表和一个名为mapA的地图列表,如下所示:
listA = ["cluster-0", "cluster-1"]
mapA = [
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-1"
}
},
{
auto_upgrade = false
disk_size_gb = 50
disk_type = "pd-balanced"
node_pool_labels = {
agentpool = "np-2"
}
},
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-3"
}
}
]
我尝试创建一个新列表,它应该如下所示:
listB = [
"cluster-0" = [
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-1"
}
},
{
auto_upgrade = false
disk_size_gb = 50
disk_type = "pd-balanced"
node_pool_labels = {
agentpool = "np-2"
}
},
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-3"
}
}
],
"cluster-1" = [
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-1"
}
},
{
auto_upgrade = false
disk_size_gb = 50
disk_type = "pd-balanced"
node_pool_labels = {
agentpool = "np-2"
}
},
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-3"
}
}
]
我尝试使用zipmap,当listA有两个元素并且mapA有两个元素时可以工作,比如只有np-1,np-2,但当我们添加np-3时会失败。试图创建这个动态列表listB。
英文:
Merge list with another list of map in terraform
We have a list listA and a list of map, mapA as below
listA = ["cluster-0","cluster-1"]
mapA = [
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-1"
}
},
{
auto_upgrade = false
disk_size_gb = 50
disk_type = "pd-balanced"
node_pool_labels = {
agentpool = "np-2"
}
},
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-3"
}
}
]
I am trying to create a new list which should look like
listB = [
"cluster-0" = [{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-1"
}
},
{
auto_upgrade = false
disk_size_gb = 50
disk_type = "pd-balanced"
node_pool_labels = {
agentpool = "np-2"
}
},
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-3"
}
}],
"cluster-1"= [{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-1"
}
},
{
auto_upgrade = false
disk_size_gb = 50
disk_type = "pd-balanced"
node_pool_labels = {
agentpool = "np-2"
}
},
{
auto_upgrade = false
disk_size_gb = 100
disk_type = "pd-standard"
node_pool_labels = {
agentpool = "np-3"
}
}]
]
I have tried zipmap which works when listA has two elements and mapA has got two elements like only np-1, np-2 but fails when we add np-3. Trying to make this dynamic listB.
答案1
得分: 1
你可以使用:
listB = [{for idx, value in local.listA: value => local.mapA }]
这样可以遍历listA
,创建一个新的列表,其中元素是一个字典,其键是listA
中的原始元素,值是mapA
。
英文:
You can use:
listB = [{for idx, value in local.listA: value => local.mapA }]
so iterate over listA
to create a new list where the elements are a dict of which the key is the original element in listA
, and the value mapA
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论