1

我正在尝试使用 Ansible 的 json_query 为特定虚拟机选择集群名称,该 json_query 在后端使用 JMESPATH。我已按照http://jmespath.org上的教程和示例进行操作,但无法构建正确的查询。

我的数据结构如下:

{
"datacenters": [
  {
  "name": "dc-a",
  "clusters": [
    { "name": "cluster-a",

      "hosts": [
        {
          "name": "host-a",
          "vms": [
            {
              "name": "vm-a",
              "summary": {
                "mem": "8",
                "diskGB": "78.00",
                "annotation": "",
                "state": "poweredOn",
                "ostype": "Microsoft Windows Server 2008 R2 (64-bit)",
                "cpu": "2"
              }
            },
            {
              "name": "vm-b",
              "summary": {
                "mem": "24",
                "diskGB": "114.00",
                "annotation": "",
                "state": "poweredOn",
                "ostype": "Microsoft Windows Server 2008 R2 (64-bit)",
                "cpu": "4"
              }
            }]
        }]
    }]
 }]
}

我可以使用下面的查询选择虚拟机('vm-a'),但我正在寻找集群(即'cluster-a')

datacenters[].clusters[].hosts[].vms[?name=='vm-a'].name[] 
4

1 回答 1

0

datacenters[].clusters[?contains(hosts[].vms[].name, 'vm-a')].name[]

于 2018-08-23T20:33:14.620 回答