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.`

1
  • file: is not a parameter for this module, i suppose its parameter path: instead Commented Oct 16, 2021 at 16:18

2 Answers 2

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 }}"
Sign up to request clarification or add additional context in comments.

Comments

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.