In a previous post , I wrote about executing Puppet from within an Ansible playbook. But the output did not look very nice. In this post I take a closer look at how to change that.
Just as a reminder, the output of Puppet looks like this, when called from inside Ansible:
Ansible offers callback plugins. And according to this gist you can use them to change Ansible’s output. So, does this also work for the Puppet output?
As stated in the docs of Ansible, the parameter callback_plugins in ansible.cfg tells Ansible where to find the plugins. On my system it is /usr/share/ansible_plugins/callback_plugins. So I create human_log.py in this directory. Calling Ansible, shows it does not work completely. It is not working for the debug task:
But do we really need the debug task? It is just a workaround to get the Puppet output when something changed or failed. With the new Ansible plugin it seems possible to realize this without the debug tasks.
Currently the plugin changes the output only for runner_on_ok and runner_on_failed. But we need it to only log for runner_on_changed. I could not find a reference Ansible implements this inside callbacks, instead Ansible hides it inside the res object. As shown in this gist , the status in runner_on_ok can be ok or changed.
Let us change the human_log.py to just print the output when something changed:
def runner_on_ok(self, host, res):
if res.pop('changed', False):
human_log(res)
else:
pass
This little change fixes the output and makes it even possible to remove the Ansible debug task. The output now looks like this when something changed:
As you can see, the plugin also removes the unicode signes, which makes it even more readable. And this is the output when everything passes:
This Ansible callback plugin is the solution to our logging problem. With this callback plugin, we can change the Puppet output and also shorten our playbook. When you got question about this topic, do not hesitate to contact me or leave a comment.
Blog author
Christian Zunker
Do you still have questions? Just send me a message.
Do you still have questions? Just send me a message.