ansibleansible-2.xansible-factsansible-awx

Ansible: how to filter json value based on another json value?


I have a query that returns:

ok: [oc-ar-ar-test-ansible-api] => {
    "ivr_list.json": [
        {
            "announcement": {
                "id": "0",
                "name": "None"
            },
            "dests": {
                "i": {
                    "id": "101",
                    "name": "Pablo Gonzalez",
                    "type": "Extension"
                },
                "t": {
                    "id": "100",
                    "name": "Fruta Peralta",
                    "type": "Extension"
                }
            },
            "id": "3",
            "loops": "2",
            "name": "Prueba",
            "timeout": "10"
        },
        {
            "announcement": {
                "id": "8",
                "name": "General-ES"
            },
            "dests": {
                "i": {
                    "id": "10000",
                    "name": "General",
                    "type": "Queue"
                },
                "t": {
                    "id": "10000",
                    "name": "General",
                    "type": "Queue"
                }
            },
            "id": "23",
            "loops": "2",
            "name": "General-ES",
            "timeout": "10"
        }
    ]
}

I need to get the "id" based on the IVR name. For example:

If the name of the IVR is "General-ES", it must return the "id" 23.

Could you help me on how to solve it, I haven't found a way to do it yet?

I did many tests but I couldn't do it, either it always returned a single ID no matter what name I entered, or I didn't get anything.


Solution

  • Thank you very much for responding, you helped me solve the problem. Here I leave the lines of code with which I solved that issue.

    - name: test1
      debug:
        msg: "{{ ivr_list.json | json_query(jmesquery) | selectattr('Name',     'equalto', 'General-ES') | map(attribute='Id') | list }}"
      vars:
        jmesquery: "[*].{Name: name, Id: id}"
      register: ivr_filter_id
      tags:
        - test