Ansible

How to parse json output in Ansible and use set_fact for variable creation

In this blog, we will explore how to parse json output in Ansible and use set_fact for variable creation based on the json output

If you want to see the video for this article, click here

Problem Statement:

  • You have an application where you want to make an api call to search for a particular group and depending on the result you want to run your playbook or set some variables

Solution:

  • Write a playbook to search for the group “test_group”
  • Use “uri” module to interact with your webservices (http/https)
  • Registering variable: When you execute a task and save the return value in a variable for use in later tasks, you create a registered variable.
  • force_basic_auth: Force the sending of the Basic authentication header upon initial request.The library used by the uri module only sends authentication information when a webservice responds to an initial request with a 401 status. Since some basic auth services do not properly send a 401, logins will fail.
  • debug: prints statements during execution and can be useful for debugging variables or expressions without necessarily halting the playbook.
  • ignore_errors: You set this as true if you want playbook to continue run subsequent tasks and doesn’t fail the playbook

Ansible Playbook

#search group if exist 
- name: Search if it exit
uri:
url: "http://{{hostname}}/api/user_groups/search?q=test_group"
method: GET
user: "username"
password: "password"
force_basic_auth: yes
return_content: yes
headers:
Content-Type: "application/json"
ignore_errors: true
register: result
- debug:
msg: "{{result}}"
- debug: 
msg: "{{ result.json | list }}"

Sample JSON Output

  • This is the sample output from my application when I execute the above playbook
"msg": {
"changed": false,
"msg": "All items completed",
"results": [
{

"content": "{\"paging\":{\"pageIndex\":1,\"pageSize\":100,\"total\":1},\"groups\":[{\"id\":11760,\"name\":\"test_group\",\"membersCount\":3,\"default\":false}]}",

"content_type": "application/json",

"json": {
"groups": [
{
"default": false,
"id": 11760,
"membersCount": 3,
"name": "test_group"
}
],
"paging": {
"pageIndex": 1,
"pageSize": 100,
"total": 1
}
}

}
]
}
  • paging.total —gives me the count of group
  • set_fact: allow setting new variables
  • Based on the count you can call other tasks/playbook etc
  • Below is the example of how you can write a playbook using set_fact and filter the json data using ‘from_json
- set_fact: total_count="{{ result.content | from_json | json_query('paging.total')}}"
- include: sentemail.yml 
when: total_groups == '0'
ignore_errors: true
  • If group is exist and you want to fetch the group name
- set_fact: group_name="{{ result.content | from_json | json_query('groups[0].name')}}"
  • Check this link for more examples of filtering in json

Congratulations, you have successfully learnt how to parse json output in Ansible and use set_fact for variable creation.

Please follow and like us: