【发布时间】: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