Search code examples
ansiblejmespath

How to use an item variable inside a JSON query in Ansible


I am trying to use an item variable inside a JSON query, but the result is an empty array.

below is the task defined for this action. But when i mention each value in the list separately in the place of item, I am getting proper value. The problem is only when I use the item variable.

    - name: Get the GP id of a Network with VLAN abc
      set_fact:
          gpmgmt: "{{gplist | json_query(\"[?Netid == `{{item}}`].{gpid: GP[?gpname == `GP-abc`].gpID}\")  }}"
      with_items: "{{ gpnet }}"
      register: gpidmgmt 

Input JSON as below(gpnet):

[
    [
        "L_12345678",
        "N_59786432"
    ]
]

Input JSON as below(gplist):

[
    {
        "GP": [],
        "Netid": "L_12345678"
    },
    {
        "GP": [
            {
                "gpID": "103",
                "gpname": "GP-abc"
            },
            {
                "gpID": "102",
                "gpname": "GP-cde"
            },
            {
                "gpID": "101",
                "gpname": "efg"
            }
        ],
        "Netid": "N_59786432"
    }
]
Output as below
TASK [debug] ****************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "changed": false, 
            "msg": "All items completed", 
            "results": [
                {
                    "ansible_facts": {
                        "gpmgmt": []
                    }, 
                    "ansible_loop_var": "item", 
                    "changed": false, 
                    "failed": false, 
                    "item": "L_12345678"
                }, 
                {
                    "ansible_facts": {
                        "gpmgmt": []
                    }, 
                    "ansible_loop_var": "item", 
                    "changed": false, 
                    "failed": false, 
                    "item": "N_59786432"
                }
            ], 
            "skipped": false
        }
    ]
}

Solution

  • i it something like this you want?:

    - name: test
      hosts: localhost
      vars:
        gpnet: [["L_12345678","N_59786432"]]
        gplist: [{"GP":[],"Netid":"L_12345678"},{"GP":[{"gpID":"103","gpname":"GP-abc"},{"gpID":"102","gpname":"GP-cde"},{"gpID":"101","gpname":"efg"}],"Netid":"N_59786432"}]
      tasks:
        - name: Get the GP id of a Network with VLAN abc
          set_fact:
            gpmgmt: "{{gpmgmt | d([]) + (gplist | json_query(query)) }}"  
          vars:
            query:  "[?Netid == '{{ item }}' ].{gpid: GP[?gpname == 'GP-abc'].gpID}"     
          loop: "{{ gpnet | flatten}}"
          register: gpidmgmt         
    
        - name: Print register
          debug: 
            msg: "{{ gpidmgmt }}" 
    
        - name: Print result
          debug: 
            msg: "{{ gpmgmt }}" 
    

    result:

    TASK [Print register] ********************************
    Sunday 07 November 2021  12:37:46 +0000 (0:00:00.058)   
    
    ok: [localhost] => 
      msg:
        changed: false
        msg: All items completed
        results:
        - ansible_facts:
            gpmgmt:
            - gpid: []
          ansible_loop_var: item
          changed: false
          failed: false
          item: L_12345678
        - ansible_facts:
            gpmgmt:
            - gpid: []
            - gpid:
              - '103'
          ansible_loop_var: item
          changed: false
          failed: false
          item: N_59786432
    
    TASK [Print result] **********************
    Sunday 07 November 2021  12:37:46 +0000 (0:00:00.094)
    
    ok: [localhost] => 
      msg:
      - gpid: []
      - gpid:
        - '103'