Reputation: 1175
I call a webservice, which returns a json response. Here is the task:
- name: calls webservice for json
uri:
url: ...
method: GET
body_format: json
register: json_response
Here is a simplified response returned by the called webservice:
{
"nodes": {
"hash1": {
"name": "host1"
},
"hash2": {
"name": "host2"
}
}
}
As you can see, nodes in the returned response is a map. Each entry represents some host. Keys are some hash values, and are not important for me. I need to check if name in any of these documents contains a specified value, eg. "host1".
How can I check if the returned JSON contains a specified host in the name field?
Upvotes: 0
Views: 183
Reputation: 4168
You can do this with the json_query filter which uses JMESPath behind the scenes.
A simple playbook for your problem would look like this:
---
- hosts: localhost
gather_facts: False
vars:
json_query: "nodes.*[] | [?name=='host1']"
json_response: |
{
"nodes": {
"hash1": {
"name": "host1"
},
"hash2": {
"name": "host2"
}
}
}
tasks:
- name: debug
debug:
msg: "{{ json_response | from_json | json_query(json_query) }}"
Upvotes: 2