【问题标题】:terraform output map value from lists of maps来自地图列表的 terraform 输出地图值
【发布时间】:2020-08-28 14:10:51
【问题描述】:

我想从 Terraform 输出中的地图列表中获取特定的地图元素。

例如,对于每个地图项,我如何访问 properties.json 中的 account 并有条件地为type 打印一个列表作为输出。

我尝试了for 循环和splat 表达式如下,但它没有返回确切的值。

好像下面的props也是一个列表。

    output "resources_by_name" {
      description = "Resource name of all machine type resources from a vRA deployment"
      value = [
        for props in deployment.deploy[*].resources.*.properties_json:
        jsondecode(props).account
        if jsondecode(props).type == "vsphere"        
        ]
    }

我不确定如何使用嵌套的 for 循环或访问列表中的地图项。

properties.json

    [
      [
        {
          "id" = "b5336bf7-07fb-4026-aa3d-479bd974ca45"
          "name" = "test1"
          "properties_json" = "{"account":"test0","constraints":"anothertest4"}"
          "type" = "vsphere"
        },
        {
          "id" = "67a3380b-8008-4f9c-9c13-2a1a935d5820"
          "name" = "test2"
          "properties_json" = "{"account":"test1","constraints":"anothertest3"}"
          "type" = "gcp"
        },
      ],
      [
        {
          "id" = "eeddd127-cba2-4b34-a2d7-e56dda5d2974"
          "name" = "test3"
          "properties_json" = "{"account":"test2","constraints":"anothertest2"}"
          "type" = "aws"
        },
        {
          "id" = "81de1857-c0c9-4c9e-8fbd-d8a1da64fa3c"
          "name" = "test4"
          "properties_json" = "{"account":"test3","constraints":"anothertest1"}"
          "type" = "az"
        },
      ],
    ]

【问题讨论】:

  • 您的 properties.json 不是有效的 json。你能仔细检查一下或提供有效的例子吗?
  • 现已更新。应该这样做。

标签: terraform


【解决方案1】:

这是工作示例。我必须填写您的问题中缺少的空白,因此您可能需要对其进行修改以满足您的需要:

locals {
  properties =     [
      [
        {
          "id" = "b5336bf7-07fb-4026-aa3d-479bd974ca45"
          "name" = "test1"
          "properties_json" = "{\"account\":\"test0\",\"constraints\":\"anothertest4\"}"
          "type" = "vsphere"
        },
        {
          "id" = "67a3380b-8008-4f9c-9c13-2a1a935d5820"
          "name" = "test2"
          "properties_json" = "{\"account\":\"test1\",\"constraints\":\"anothertest3\"}"
          "type" = "gcp"
        },
      ],
      [
        {
          "id" = "eeddd127-cba2-4b34-a2d7-e56dda5d2974"
          "name" = "test3"
          "properties_json" = "{\"account\":\"test2\",\"constraints\":\"anothertest2\"}"
          "type" = "aws"
        },
        {
          "id" = "81de1857-c0c9-4c9e-8fbd-d8a1da64fa3c"
          "name" = "test4"
          "properties_json" = "{\"account\":\"test3\",\"constraints\":\"anothertest1\"}"
          "type" = "az"
        },
      ],
    ]
}

output "resources_by_name" {
  value = [for props in flatten(local.properties):
           jsondecode(props.properties_json).account 
             if props.type == "vsphere"   
          ]
}

结果:

resources_by_name = [
  "test0",
]

【讨论】:

    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2020-11-17
    • 2019-08-24
    • 2017-07-23
    相关资源
    最近更新 更多