Human-Readable Ansible Playbook Log Output Using Callback Plugin

One problem I’ve had with Ansible playbook since its early 0.x days is with its verbose log output. Jsonified by default, it’s hard to read, and pretty much impossible for a human to review when its stdout or stderr contains tens/hundreds of lines combined into one lengthy string.

Here’s how it looks like:

changed: [gennou.local] => {"changed": true, "cmd": "/tmp/sample.sh", "delta": "0:00:00.019164", "end": "2014-03-30 21:05:33.994066", "rc": 0, "start": "2014-03-30 21:05:33.974902", "stderr": "", "stdout": "gazillion texts here with lots of \n in between gazillion texts here with lots of \n in between gazillion texts here with lots of \n in between gazillion texts here with lots \n in between"}

When –verbose flag is set, I believe that the intention is for a human to eventually review the verbose log output. And whenever the human did review the log, the person never failed to tell me that the jsonified message was impossible to read, to which I replied with “They will fix it someday.”

Well, Ansible is now at version 1.x and the problem is still there.

So, while we continue on waiting, the workaround I use for now is to set up an Ansible callback plugin that listens to some task events, and then logs the result in a human readable format, with each field on its own line and newline stays as-is.

Here’s how I set it up:

  1. Set callback plugins directory in Ansible configuration file (ansible.cfg file):
[defaults] callback_plugins = path/to/callback_plugins/
  1. Create a callback plugin file at path/to/callback_plugins/ directory, I call mine human_log.py . Here’s the callback plugin gist: https://gist.github.com/cliffano/9868180

  2. Run ansible-playbook command:

ansible-playbook -i hosts playbook.yml

And the log output looks like this:

cmd: /tmp/sample.sh start: 2014-03-30 21:05:33.974902 end: 2014-03-30 21:05:33.994066 delta: 0:00:00.019164 stdout: gazillion texts here with lots of in between gazillion texts here with lots of in between gazillion texts here with lots of in between gazillion texts here with lots of in between stderr:

Now that’s more readable.

You can set the callback plugin on each Ansible project if you want to. But I set mine as part of my CI/Jenkins boxes provisioning, that way all Jenkins jobs that execute an Ansible playbook end up with a readable log output.

Note: I know that some people suggest using debug module to split output into multiple lines. However, having to add register and debug fields all over the tasks would easily clutter the playbook. I find the callback plugin to be a cleaner and simpler solution.

Share Comments
comments powered by Disqus