Hello Everyone,
In this blog post lets see how to execute ansible-playbook with custom output format like JSON
Lets say you have a playbook like below
---
- hosts: 127.0.0.1
gather_facts: false
tasks:
- name: execute uptime command
command: uptimer
register: result
- debug: var=result.stdout
And if you execute the playbook with below command
ansible-playbook playbook.yaml
You will get normal ansible standard output format like below
▶ ansible-playbook ansible_uptime.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
_______________________________
< TASK [execute uptime command] >
-------------------------------
changed: [127.0.0.1]
ok: [127.0.0.1] => {
"result.stdout": "12:34 up 4:03, 2 users, load averages: 3.42 3.62 3.10"
}
127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
~/my_learning/ansible
If you want output in JSON
format for example, you can use ANSIBLE_STDOUT_CALLBACK
env variable. Set the variable value with respect to output format you want. If its JSON
ANSIBLE_STDOUT_CALLBACK=json ansible-playbook ansible_uptime.yaml
▶ ANSIBLE_STDOUT_CALLBACK=json ansible-playbook ansible_uptime.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
{
"custom_stats": {},
"global_custom_stats": {},
"plays": [
{
"play": {
"duration": {
"end": "2021-10-26T07:06:58.968665Z",
"start": "2021-10-26T07:06:58.419247Z"
},
"id": "acde4800-1122-4989-e4cc-000000000005",
"name": "127.0.0.1"
},
"tasks": [
{
"hosts": {
"127.0.0.1": {
"_ansible_no_log": false,
"action": "command",
"changed": true,
"cmd": [
"uptime"
],
"delta": "0:00:00.008988",
"end": "2021-10-26 12:36:58.904425",
"invocation": {
"module_args": {
"_raw_params": "uptime",
"_uses_shell": false,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": false
}
},
"msg": "",
"rc": 0,
"start": "2021-10-26 12:36:58.895437",
"stderr": "",
"stderr_lines": [],
"stdout": "12:36 up 4:05, 2 users, load averages: 3.78 3.54 3.14",
"stdout_lines": [
"12:36 up 4:05, 2 users, load averages: 3.78 3.54 3.14"
]
}
},
"task": {
"duration": {
"end": "2021-10-26T07:06:58.942858Z",
"start": "2021-10-26T07:06:58.448326Z"
},
"id": "acde4800-1122-4989-e4cc-000000000007",
"name": "execute uptime command"
}
},
{
"hosts": {
"127.0.0.1": {
"_ansible_no_log": false,
"_ansible_verbose_always": true,
"action": "debug",
"changed": false,
"result.stdout": "12:36 up 4:05, 2 users, load averages: 3.78 3.54 3.14"
}
},
"task": {
"duration": {
"end": "2021-10-26T07:06:58.968665Z",
"start": "2021-10-26T07:06:58.949451Z"
},
"id": "acde4800-1122-4989-e4cc-000000000008",
"name": "debug"
}
}
]
}
],
"stats": {
"127.0.0.1": {
"changed": 1,
"failures": 0,
"ignored": 0,
"ok": 2,
"rescued": 0,
"skipped": 0,
"unreachable": 0
}
}
}
~/my_learning/ansible
Hope it helps.
Thank you.
Raaz