Processing ansible's output in shell script

Hi All,

I have a shell script from which I am invoking an ansible playbook.

My playbook checks for some status of my services and I want this information to be available in my shell script after playbook execution is complete.

I have to share data from ansible to shell script that invoked the playbook.

Any idea how can we do that?

Thanks

You might want to use a custom callback to modify the output to a format you want to consume.

Hi Brian, can you please provide me with an example or any link to samples of this custom call back?

https://docs.ansible.com/ansible/developing_plugins.html#callbacks

The callbacks don’t work as using callback I am not able to remove messages like

`

PLAY [all] *********************************************************************

TASK [cmx_running_services] ****************************************************

PLAY RECAP *********************************************************************
172.31.35.225 : ok=2 changed=0 unreachable=0 failed=0
172.31.9.253 : ok=2 changed=0 unreachable=0 failed=0

`

I want my playbook to output only in json format and no such text like above

Have you tried json call_back_plugin, it does exactly that?

https://docs.ansible.com/ansible/intro_configuration.html#stdout-callback

Yes I tried it and it outputs json content for each task. Something like this:

`

TASK [create some file] ********************************************************
ok: [172.31.9.253 → localhost]
{“group”: “ubuntu”, “_ansible_no_log”: false, “_ansible_delegated_vars”: {“ansible_host”: “localhost”}, “changed”: false, “host”: “172.31.9.253”, “invocation”: {“module_args”: {“directory_mode”: null, “force”: false, “remote_src”: null, “path”: “/tmp/services_status.json.tmp”, “owner”: null, “follow”: true, “group”: null, “state”: null, “content”: null, “serole”: null, “diff_peek”: null, “setype”: null, “dest”: “/tmp/services_status.json.tmp”, “selevel”: null, “original_basename”: “services.j2”, “regexp”: null, “validate”: null, “src”: null, “seuser”: null, “recurse”: false, “delimiter”: null, “mode”: null, “backup”: null}}, “state”: “file”, “gid”: 1000, “mode”: “0664”, “diff”: {“after”: {“path”: “/tmp/services_status.json.tmp”}, “before”: {“path”: “/tmp/services_status.json.tmp”}}, “owner”: “ubuntu”, “path”: “/tmp/services_status.json.tmp”, “size”: 542, “uid”: 1000}
ok: [172.31.35.225 → localhost]
{“group”: “ubuntu”, “_ansible_no_log”: false, “_ansible_delegated_vars”: {“ansible_host”: “localhost”}, “changed”: false, “host”: “172.31.35.225”, “invocation”: {“module_args”: {“directory_mode”: null, “force”: false, “remote_src”: null, “path”: “/tmp/services_status.json.tmp”, “owner”: null, “follow”: true, “group”: null, “state”: null, “content”: null, “serole”: null, “diff_peek”: null, “setype”: null, “dest”: “/tmp/services_status.json.tmp”, “selevel”: null, “original_basename”: “services.j2”, “regexp”: null, “validate”: null, “src”: null, “seuser”: null, “recurse”: false, “delimiter”: null, “mode”: null, “backup”: null}}, “state”: “file”, “gid”: 1000, “mode”: “0664”, “diff”: {“after”: {“path”: “/tmp/services_status.json.tmp”}, “before”: {“path”: “/tmp/services_status.json.tmp”}}, “owner”: “ubuntu”, “path”: “/tmp/services_status.json.tmp”, “size”: 542, “uid”: 1000}

TASK [debug] *******************************************************************

`

This is not helpful to me as I only want json output and no thing in between, because in shell script I want to read this output and convert the output to json object.
Please let me know if I am not clear with my requirement.

When I run with the json callback in 2.1.1 I only get json output in return. Your output indicate you are not using json callback_plugin.
So how do you set up Ansible to use json callback?

I just added in my ansible.cfg file:

`

stdout_callback = skippy
callback_plugins = /home/ubuntu/projects/package/target/ansible/plugins/callback

`

Downloaded plugin from https://github.com/petems/ansible-json and added it in the above folder.
Now when I execute the playbook, I get the kind of output I mentioned earlier

I also tried using plugin from https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/json.py

But it gives me syntax error in “json.py” when I execute my playbook. I think it requires Python 3, I am having python 2.7.6 and I can’t upgrade to python 3

I am using Ansible version 2.1.1.0

Ansible comes with the json callback plugins and some others you'll find them in /usr/lib/python2.7/dist-packages/ansible/plugins/callback/

To use the provided json callback set "stdout_callback = json" and remove the "callback_plugins =" in your ansible.cfg. Then you should only get a json output.

wow it worked :slight_smile: thanks Kai

However just one last question, I have multiple playbook but I want this json out only for one. So I cant have this config in ansible.cfg
Is there a way to pass this config other than editing this ansible.cfg file?

use the ANSIBLE_STDOUT_CALLBACK environment varialbe:

ANSIBLE_STDOUT_CALLBACK=json ansible-playbook …