I have an Ansible task that makes a URI request to a website to get a JSON response. I want Ansible to do something if the nested JSON variable is defined, and something else if it isn't.
- name: Get JSON from the Interwebs
uri: url="http://whatever.com/jsonresponse" return_content=yes
register: json_response
- name: Write nested JSON variable to disk
copy: content={{json_response.json.nested1.nested2}} dest="/tmp/foo.txt"
Note that using ignore_errors
only works for the task's command failing, not for checking undefined values in nested data structures within a Jinja template. So if json_response.json.nested1.nested2
isn't defined, this task will still fail despite ignore_errors=yes
being set.
How do I get this playbook to store some default value in /tmp/foo.txt
if the request fails, or if the request doesn't have the right nested value defined?