Get output from ansible.playbook.PlayBook()

I’m executing a playbook from a Flask web service. All output goes to stdout console. I need it in my webapp so that I can send it back to the browser. Best I can get on the server is the summary stats counter from the pb object. (success,skipped,failed,etc)

How can I get the complete output like with ansible.Runner()?

stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats,verbose=utils.VERBOSITY)

pb = ansible.playbook.PlayBook(
playbook = playbook
stats=stats,
inventory=targets,
callbacks=playbook_cb,
runner_callbacks=runner_cb
).run()

Thanks!

Moving this to ansible-devel.

You could always capture stdout and stderr via something like:

import sys
from cStringIO import StringIO

sys_stdout = sys.stdout
sys_stderr = sys.stderr

sys.stdout = StringIO()
sys.stderr = StringIO()

run ansible.playbook.Playbook here

stdout = sys.stdout.getvalue()
stderr = sys.stderr.getvalue()

sys.stdout = sys_stdout
sys.stderr = sys_stderr

There are a number of different ways to go about this. Including, creating your own callback class and registering that instead.

Ansible uses callback plugins for everything, so basically you’re going to need to write one.

I recommend Ansible Tower for easier API access for integrating with web applications - not only is the REST API very well designed for this, you avoid possible licensing issues if redistributing your app, can build your apps in any language (not just Python), don’t have to deal with resource scheduling (already there), and also don’t have to deal with a possibly changing callback internals.