用json_query / jmespath过滤

时间:2018-06-25 09:28:53

标签: ansible jmespath

我使用json_query / jmespath尝试从以下JSON中选择items.metadata,名称和status.loadBalancer.ingress.ip:

Vue.axios.get("/?do=vueTestComponent-ReloadTime", {
  withCredentials: true,
}).then(...)

我要获取的输出如下所示:

{
"apiVersion": "v1",
"items": [
    {
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {
            "annotations": {
                "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{\"service.beta.kubernetes.io/azure-load-balancer-internal\":\"true\"},\"name\":\"kong-admin-internal\",\"namespace\":\"co-ie\"},\"spec\":{\"loadBalancerSourceRanges\":[\"0.0.0.0/0\"],\"ports\":[{\"name\":\"kong-admin\",\"port\":8444,\"protocol\":\"TCP\",\"targetPort\":8444}],\"selector\":{\"app\":\"kong-internal\"},\"type\":\"LoadBalancer\"}}\n",
                "service.beta.kubernetes.io/azure-load-balancer-internal": "true"
            },
            "creationTimestamp": "2018-06-24T20:31:40Z",
            "name": "kong-admin-internal",
            "namespace": "coe",
            "resourceVersion": "7789",
            "selfLink": "/api/v1/namespaces/co-ie/services/kong-admin-internal",
            "uid": "998b54a6-77ed-11e8-b1c0-000d3a0430a9"
        },
        "spec": {
            "clusterIP": "182.168.0.1",
            "externalTrafficPolicy": "Cluster",
            "loadBalancerSourceRanges": [
                "0.0.0.0/0"
            ],
            "ports": [
                {
                    "name": "kong-admin",
                    "nodePort": 30318,
                    "port": 8444,
                    "protocol": "TCP",
                    "targetPort": 8444
                }
            ],
            "selector": {
                "app": "kong-internal"
            },
            "sessionAffinity": "None",
            "type": "LoadBalancer"
        },
        "status": {
            "loadBalancer": {
                "ingress": [
                    {
                        "ip": "172.16.22.5"
                    }
                ]
            }
        }
    },
    {
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {
            "annotations": {
                "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{},\"name\":\"kong-proxy-external\",\"namespace\":\"co-ie\"},\"spec\":{\"loadBalancerSourceRanges\":[\"0.0.0.0/0\"],\"ports\":[{\"name\":\"kong-proxy\",\"port\":8443,\"protocol\":\"TCP\",\"targetPort\":8443}],\"selector\":{\"app\":\"kong-external\"},\"type\":\"LoadBalancer\"}}\n"
            },
            "creationTimestamp": "2018-06-24T20:31:35Z",
            "name": "kong-proxy-external",
            "namespace": "co-ie",
            "resourceVersion": "7746",
            "selfLink": "/api/v1/namespaces/co-ie/services/kong-proxy-external",
            "uid": "9679159f-77ed-11e8-b1c0-000d3a0430a9"
        },
        "spec": {
            "clusterIP": "192.168.0.3",
            "externalTrafficPolicy": "Cluster",
            "loadBalancerSourceRanges": [
                "0.0.0.0/0"
            ],
            "ports": [
                {
                    "name": "kong-proxy",
                    "nodePort": 32099,
                    "port": 8443,
                    "protocol": "TCP",
                    "targetPort": 8443
                }
            ],
            "selector": {
                "app": "kong-external"
            },
            "sessionAffinity": "None",
            "type": "LoadBalancer"
        },
        "status": {
            "loadBalancer": {
                "ingress": [
                    {
                        "ip": "192.168.0.2"
                    }
                ]
            }
        }
    }
],
"kind": "List",
"metadata": {
    "resourceVersion": "",
    "selfLink": ""
}
}

通过以下查询,我已经非常接近

[
  {
    "name": "kong-admin-internal",
    "ip": "172.16.22.5"
  },
  {
    "name": "kong-proxy-external",
    "ip": "192.168.0.2"
  }
]

哪个给我:

items[?contains(metadata.name, 'kong' ) == `true`].{name: metadata.name, ip: status.loadBalancer.ingress[].ip}

我不确定如何完成将IP设置为字符串而不是列表的最后一步

0 个答案:

没有答案