0

I have a json query that returns this result when run using set_fact and displaying using debug:

'''
Json_query: hits | community.general.json_query('results[?item==`xyz`].count')
debug output: TASK [debug] **********************************************************************************************************************************************
ok: [control] => {
    "new_list8": "0"
}

Variable used in when conditional   
matched: ["0"]

'''

In the JSON_query, "xyz" needs to be substituted by a loop variable in when conditional:

    
 ```
 **- name: test
      community.general.xml:
        file: '/home/cloud_user/ansible/ansible_playbook/dev/xmlfiles/test.xml'
        #backup: yes
        pretty_print: true
        xpath: /x:AgentMap
        namespaces:
          x: http://xyz
        add_children:
          - env:
              name: "{{ server|upper }}"
              _:
                - agent:
                    _:
                      - name: "{{ server }}"

      with_items:
        - "{{ servers }}"
      when: matched[0] in (hits | community.general.json_query('results[?item==`\" + server + \"`].count'))
      loop_control:
        loop_var: server**
```

Loop variable has been mentioned as seen in

<https://stackoverflow.com/questions/46038985/ansible-pass-a-variable-in-a-json-query- filter>

When condition always evaluates to false.Task is to add a server to file if it doesn't already exist in it. Mentioned JSON_QUERY is to extract the count value from another task that checks number of occurrences. Query in JPTERM works fine too.`

Aksha
  • 11
  • 2

2 Answers2

0

fo your problem i rewrite your task like this:

 ```
 **- name: test
      community.general.xml:
        file: '/home/cloud_user/ansible/ansible_playbook/dev/xmlfiles/test.xml'
        #backup: yes
        pretty_print: true
        xpath: /x:AgentMap
        namespaces:
          x: http://xyz
        add_children:
          - env:
              name: "{{ item|upper }}"
              _:
                - agent:
                    _:
                      - name: "{{ item }}"

      when: matched[0] in (hits | community.general.json_query('results[?item==`\" ~ item ~ \"`].count'))
      with_items:
        - "{{ servers }}"
Frenchy
  • 16,386
  • 3
  • 16
  • 39
0

you cannot use an ansible variable inside the json_query.

specify the jmespath statement in a variable,

then use that in the json_query

- name: test
  community.general.xml:
        file: '/home/cloud_user/ansible/ansible_playbook/dev/xmlfiles/test.xml'
        #backup: yes
        pretty_print: true
        xpath: /x:AgentMap
        namespaces:
          x: http://xyz
        add_children:
          - env:
              name: "{{ server|upper }}"
              _:
                - agent:
                    _:
                      - name: "{{ server }}"

  loop: "{{ servers }}"
  when: matched[0] in (hits | community.general.json_query(my_query)
  loop_control:
    loop_var: server
  vars:
    my_query: 'results[?item==`{{ server }}`].count'
Rob
  • 1
  • 2