我有这样的输出:
output "esxi_gw_ip" {
value = "${packet_device.esxi.network}"
}
结果是这样的:
"outputs": {
"esxi_gw_ip": {
"sensitive": false,
"type": "list",
"value": [
{
"address": "139.0.0.2",
"cidr": "29",
"family": "4",
"gateway": "139.0.0.1",
"public": "1"
},
{
"address": "blah",
"cidr": "127",
"family": "6",
"gateway": "blah",
"public": "1"
},
{
"address": "10.88.94.2",
"cidr": "29",
"family": "4",
"gateway": "10.88.94.1",
"public": "0"
}
]
}
我想从family = 4
和public = 1
获取网关...我该怎么做?我可以像这样从列表中获得第一个,然后在jq
中使用类似local-exec
的东西:
output "esxi_gw_ip" {
value = "${packet_device.esxi.network[0]}"
}
但是这并不能保证它会一直0
,而且我也在尝试以terraform与使用shell的方式自然地做到这一点……
答案 0 :(得分:1)
解决了我的问题!如果有人感兴趣..这是我的操作方法....在main.tf中,我添加了以下内容:
data "template_file" "packet_gw_public" {
count = "${length(packet_device.esxi.network)}"
template = "${lookup(packet_device.esxi.network[count.index], "public") == 1 && lookup(packet_device.esxi.network[count.index], "family") == "4" ? lookup(packet_device.esxi.network[count.index], "gateway") : "" }"
}
然后在output.tf中添加:
output "esxi_gw_ip" {
value = "${element(compact(data.template_file.packet_gw_public.*.rendered),0)}"
}