Processing ansible's output in shell script

2,718 views
Skip to first unread message

ankur...@gmail.com

unread,
Aug 25, 2016, 10:57:53 AM8/25/16
to Ansible Project
Hi All,

I have shell script that invokes a playbook.
This playbook is check status of my services. 

What I want to achieve is, after execution of playbook I want this status information in my shell script so that I can do some stuff based on status.

So how can we pass data from playbook to shell script that invoked it?

Thanks

ankur...@gmail.com

unread,
Aug 25, 2016, 10:57:53 AM8/25/16
to Ansible Project
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

Brian Coca

unread,
Aug 25, 2016, 11:23:34 AM8/25/16
to ansible...@googlegroups.com
You might want to use a custom callback to modify the output to a format you want to consume.

----------
Brian Coca

ankur...@gmail.com

unread,
Aug 25, 2016, 4:24:07 PM8/25/16
to Ansible Project
Hi Brian, can you please provide me with an example or any link to samples of this custom call back? 

Kai Stian Olstad

unread,
Aug 25, 2016, 4:37:04 PM8/25/16
to ansible...@googlegroups.com
On 25. aug. 2016 22:13, ankur...@gmail.com wrote:
> 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

--
Kai Stian Olstad

ankur...@gmail.com

unread,
Aug 26, 2016, 5:33:34 AM8/26/16
to Ansible Project, ansible-pr...@olstad.com
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

Kai Stian Olstad

unread,
Aug 26, 2016, 6:06:37 AM8/26/16
to ansible...@googlegroups.com
On 26. aug. 2016 11:33, ankur...@gmail.com wrote:
> 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

--
Kai Stian Olstad

ankur...@gmail.com

unread,
Aug 26, 2016, 6:45:25 AM8/26/16
to Ansible Project, ansible-pr...@olstad.com
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.

Kai Stian Olstad

unread,
Aug 26, 2016, 6:55:34 AM8/26/16
to ansible...@googlegroups.com
On 26. aug. 2016 12:45, ankur...@gmail.com wrote:
> 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?

--
Kai Stian Olstad

ankur...@gmail.com

unread,
Aug 26, 2016, 7:33:32 AM8/26/16
to Ansible Project, ansible-pr...@olstad.com
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


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

Kai Stian Olstad

unread,
Aug 26, 2016, 7:53:34 AM8/26/16
to ansible...@googlegroups.com
On 26. aug. 2016 13:33, ankur...@gmail.com wrote:
> 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.

--
Kai Stian Olstad

ankur...@gmail.com

unread,
Aug 26, 2016, 8:38:32 AM8/26/16
to Ansible Project, ansible-pr...@olstad.com
wow it worked :) 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?

Brian Coca

unread,
Aug 26, 2016, 9:05:21 AM8/26/16
to ansible...@googlegroups.com, ansible-pr...@olstad.com
use the ANSIBLE_STDOUT_CALLBACK environment varialbe:

ANSIBLE_STDOUT_CALLBACK=json ansible-playbook ....


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages