Terraform 从地图列表中的地图元素拾取的地形变量输出

Terraform 从地图列表中的地图元素拾取的地形变量输出,terraform,Terraform,我有这样一个输出: output "esxi_gw_ip" { value = "${packet_device.esxi.network}" } 结果是: "outputs": { "esxi_gw_ip": { "sensitive": false, "type": "list", "value": [

我有这样一个输出:

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
获取网关……我该怎么做?我可以像这样从列表中获取第一个,然后在
localexec
中使用类似
jq
的内容:

output "esxi_gw_ip" {
  value = "${packet_device.esxi.network[0]}"
}

但这并不能保证它始终是
0
,我也尝试在terraform和使用shell中本机实现这一点…

解决了我的问题!如果有人感兴趣的话……以下是我是如何做到的……在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 "esxi_gw_ip" {
  value = "${element(compact(data.template_file.packet_gw_public.*.rendered),0)}"
}
然后在output.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 "esxi_gw_ip" {
  value = "${element(compact(data.template_file.packet_gw_public.*.rendered),0)}"
}