'How to select nested values?

The following is the output (return values) of an Ansible module (Oracle Cloud Networking Module). I want to get nested values. I tried with:

var=result_pub_ips_reserved.public_ips.assigned_entity_id

but this does not work. Am I missing something?

Code:

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"
            }
        ]
    }
}


Solution 1:[1]

your public_ips is an array so you have to notify the index: (here index is 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

result:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1