如何在Terraform中创建一个动态列表,将字符串列表与映射列表合并?

huangapple go评论62阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月1日 18:36:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定