Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inspect a json response from Ansible URI call

I have a service call that returns system status in json format. I want to use the ansible URI module to make the call and then inspect the response to decide whether the system is up or down

{"id":"20161024140306","version":"5.6.1","status":"UP"}

This would be the json that is returned

This is the ansible task that makes a call:

 - name: check sonar web is up
   uri:
    url: http://sonarhost:9000/sonar/api/system/status
    method: GET
    return_content: yes
    status_code: 200
    body_format: json
    register: data

Question is how can I access data and inspect it as per ansible documentation this is how we store results of a call. I am not sure of the final step which is to check the status.

like image 684
Hafiz Avatar asked Oct 25 '16 08:10

Hafiz


1 Answers

This works for me.

- name: check sonar web is up
uri:
  url: http://sonarhost:9000/sonar/api/system/status
  method: GET
  return_content: yes
  status_code: 200
  body_format: json
register: result
until: result.json.status == "UP"
retries: 10
delay: 30

Notice that result is a ansible dictionary and when you set return_content=yes the response is added to this dictionary and is accessible using json key

Also ensure you have indented the task properly as shown above.

like image 100
Hafiz Avatar answered Oct 03 '22 08:10

Hafiz