如何选择名字等于孩子名字的父母

时间:2019-11-02 14:15:32

标签: jq

我以为这很简单,但是我很困惑。如何为每个子hostvars条目选择父activedirectory_devops.hosts条目?如果这是SQL,我会尝试编写一个join

    {
        "_meta": {
            "hostvars": {
                "foo_423d1fe9-d964-d45e-ec69-2565589deae6": {
                "ansible_host": "192.168.0.1",
                "config.cpuHotAddEnabled": true,
                "config.cpuHotRemoveEnabled": false,
                "config.hardware.numCPU": 4,
                "config.instanceUuid": "503d1a90-8b07-4b33-2648-5d1f9330e5f7",
                "config.name": "devops-dal13-bc1",
                "config.template": false,
                "guest.guestId": "windows9Server64Guest",
                "guest.guestState": "running",
                "guest.hostName": "foo.company.com",
                "guest.ipAddress": "192.168.0.1",
                "inventory_dir": "/ansible/inventories",
                "inventory_file": "/ansible/inventories/vcsa2.vmware.yml",
                "name": "foo",
                "runtime.maxMemoryUsage": 16384
            },
,
                "bar_420b8b36-fffe-6f29-3e32-0ce9aa0d0ad3": {
                    "config.cpuHotAddEnabled": false,
                    "config.cpuHotRemoveEnabled": false,

...
            }
        }
    },
    "activedirectory_devops": {
        "hosts": [
            "foo_423d1fe9-d964-d45e-ec69-2565589deae6",
            "bar_420b8b36-fffe-6f29-3e32-0ce9aa0d0ad3"
        ]
    },

我未完成的尝试:

jq '. as $parent | .activedirectory_devops.hosts as $children | .'

1 个答案:

答案 0 :(得分:0)

  

如何为每个子activedirectory_devops.hosts条目选择父hostvars条目?

以下jq查询完全可以做到这一点:

.activedirectory_devops.hosts[] as $host
| ._meta.hostvars[$host]