如何select嵌套值?

How to select nested values?

以下是 Ansible 模块(Oracle 云网络模块)的输出(return 值)。我想获得嵌套值。我试过:

var=result_pub_ips_reserved.public_ips.assigned_entity_id

但这不起作用。我错过了什么吗?

代码:

ok: [localhost] => {
    "result_pub_ips_reserved": {
        "changed": false,
        "failed": false,
        "public_ips": [
            {
                "assigned_entity_id": "ocid1.privateip.oc1.eu-frankfurt-1.abtheljtfhzqfze7tvfgbesdkytmf6xaiegwjfjcpw73gamozymt6jh2xrya",
                "assigned_entity_type": "PRIVATE_IP",
                "availability_domain": null,
                "compartment_id": "ocid1.compartment.oc1..aaaaaaaadnurmgbsrwgao42kohyg6t7qil35c2hkjrhicchnltra45fwavma",
                "defined_tags": {},
                "display_name": "anakin-pub-ip",
                "freeform_tags": {},
                "id": "ocid1.publicip.oc1.eu-frankfurt-1.amaaaaaafzy3a4ya2af5l26fnrjvq2tr4mmipqdqoei5o7o72ir5h5olds4a",
                "ip_address": "144.24.171.161",
                "lifecycle_state": "ASSIGNED",
                "lifetime": "RESERVED",
                "private_ip_id": "ocid1.privateip.oc1.eu-frankfurt-1.abtheljtfhzqfze7tvfgbesdkytmf6xaiegwjfjcpw73gamozymt6jh2xrya",
                "public_ip_pool_id": null,
                "scope": "REGION",
                "time_created": "2022-02-22T12:34:35.878000+00:00"
            }
        ]
    }
}

你的public_ips是一个数组所以你必须通知索引:(这里索引是0)

result_pub_ips_reserved.public_ips.0.assigned_entity_id

- name: "make this working"
  hosts: localhost
  vars:
    result_pub_ips_reserved:
      changed: false
      failed: false
      public_ips:
      - assigned_entity_id: ocid1.privateip.oc1.eu-frankfurt-1.abtheljtfhzqfze7tvfgbesdkytmf6xaiegwjfjcpw73gamozymt6jh2xrya
        assigned_entity_type: PRIVATE_IP
        availability_domain:
        compartment_id: ocid1.compartment.oc1..aaaaaaaadnurmgbsrwgao42kohyg6t7qil35c2hkjrhicchnltra45fwavma
        defined_tags: {}
        display_name: anakin-pub-ip
        freeform_tags: {}
        id: ocid1.publicip.oc1.eu-frankfurt-1.amaaaaaafzy3a4ya2af5l26fnrjvq2tr4mmipqdqoei5o7o72ir5h5olds4a
        ip_address: 144.24.171.161
        lifecycle_state: ASSIGNED
        lifetime: RESERVED
        private_ip_id: ocid1.privateip.oc1.eu-frankfurt-1.abtheljtfhzqfze7tvfgbesdkytmf6xaiegwjfjcpw73gamozymt6jh2xrya
        public_ip_pool_id:
        scope: REGION
        time_created: '2022-02-22T12:34:35.878000+00:00'

  tasks:

    - debug:
        var: result_pub_ips_reserved.public_ips.0.assigned_entity_id

结果:

ok: [localhost] => {
    "result_pub_ips_reserved.public_ips.0.assigned_entity_id": "ocid1.privateip.oc1.eu-frankfurt-1.abtheljtfhzqfze7tvfgbesdkytmf6xaiegwjfjcpw73gamozymt6jh2xrya"
}

"public_ips" 是一个只有一个元素的数组:散列。您需要访问该数组的第一个元素 result_pub_ips_reserved.public_ips.0.assigned_entity_id,或删除 {...}

周围的 [...]