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

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

How can I create a dynamic list in Terraform that combines a list of strings with a list of maps?

问题

合并具有另一个地图列表的列表在terraform中

我们有一个名为listA的列表和一个名为mapA的地图列表,如下所示:

  1. listA = ["cluster-0", "cluster-1"]
  1. mapA = [
  2. {
  3. auto_upgrade = false
  4. disk_size_gb = 100
  5. disk_type = "pd-standard"
  6. node_pool_labels = {
  7. agentpool = "np-1"
  8. }
  9. },
  10. {
  11. auto_upgrade = false
  12. disk_size_gb = 50
  13. disk_type = "pd-balanced"
  14. node_pool_labels = {
  15. agentpool = "np-2"
  16. }
  17. },
  18. {
  19. auto_upgrade = false
  20. disk_size_gb = 100
  21. disk_type = "pd-standard"
  22. node_pool_labels = {
  23. agentpool = "np-3"
  24. }
  25. }
  26. ]

我尝试创建一个新列表,它应该如下所示:

  1. listB = [
  2. "cluster-0" = [
  3. {
  4. auto_upgrade = false
  5. disk_size_gb = 100
  6. disk_type = "pd-standard"
  7. node_pool_labels = {
  8. agentpool = "np-1"
  9. }
  10. },
  11. {
  12. auto_upgrade = false
  13. disk_size_gb = 50
  14. disk_type = "pd-balanced"
  15. node_pool_labels = {
  16. agentpool = "np-2"
  17. }
  18. },
  19. {
  20. auto_upgrade = false
  21. disk_size_gb = 100
  22. disk_type = "pd-standard"
  23. node_pool_labels = {
  24. agentpool = "np-3"
  25. }
  26. }
  27. ],
  28. "cluster-1" = [
  29. {
  30. auto_upgrade = false
  31. disk_size_gb = 100
  32. disk_type = "pd-standard"
  33. node_pool_labels = {
  34. agentpool = "np-1"
  35. }
  36. },
  37. {
  38. auto_upgrade = false
  39. disk_size_gb = 50
  40. disk_type = "pd-balanced"
  41. node_pool_labels = {
  42. agentpool = "np-2"
  43. }
  44. },
  45. {
  46. auto_upgrade = false
  47. disk_size_gb = 100
  48. disk_type = "pd-standard"
  49. node_pool_labels = {
  50. agentpool = "np-3"
  51. }
  52. }
  53. ]

我尝试使用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

  1. listA = ["cluster-0","cluster-1"]
  1. mapA = [
  2. {
  3. auto_upgrade = false
  4. disk_size_gb = 100
  5. disk_type = "pd-standard"
  6. node_pool_labels = {
  7. agentpool = "np-1"
  8. }
  9. },
  10. {
  11. auto_upgrade = false
  12. disk_size_gb = 50
  13. disk_type = "pd-balanced"
  14. node_pool_labels = {
  15. agentpool = "np-2"
  16. }
  17. },
  18. {
  19. auto_upgrade = false
  20. disk_size_gb = 100
  21. disk_type = "pd-standard"
  22. node_pool_labels = {
  23. agentpool = "np-3"
  24. }
  25. }
  26. ]

I am trying to create a new list which should look like

  1. listB = [
  2. "cluster-0" = [{
  3. auto_upgrade = false
  4. disk_size_gb = 100
  5. disk_type = "pd-standard"
  6. node_pool_labels = {
  7. agentpool = "np-1"
  8. }
  9. },
  10. {
  11. auto_upgrade = false
  12. disk_size_gb = 50
  13. disk_type = "pd-balanced"
  14. node_pool_labels = {
  15. agentpool = "np-2"
  16. }
  17. },
  18. {
  19. auto_upgrade = false
  20. disk_size_gb = 100
  21. disk_type = "pd-standard"
  22. node_pool_labels = {
  23. agentpool = "np-3"
  24. }
  25. }],
  26. "cluster-1"= [{
  27. auto_upgrade = false
  28. disk_size_gb = 100
  29. disk_type = "pd-standard"
  30. node_pool_labels = {
  31. agentpool = "np-1"
  32. }
  33. },
  34. {
  35. auto_upgrade = false
  36. disk_size_gb = 50
  37. disk_type = "pd-balanced"
  38. node_pool_labels = {
  39. agentpool = "np-2"
  40. }
  41. },
  42. {
  43. auto_upgrade = false
  44. disk_size_gb = 100
  45. disk_type = "pd-standard"
  46. node_pool_labels = {
  47. agentpool = "np-3"
  48. }
  49. }]
  50. ]

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

你可以使用:

  1. listB = [{for idx, value in local.listA: value => local.mapA }]

这样可以遍历listA,创建一个新的列表,其中元素是一个字典,其键是listA中的原始元素,值是mapA

英文:

You can use:

  1. 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:

确定