Debugging Ansible Modules

From time to time, you might want to debug an ansible module that you are developing. Simply adding ‘print’ statements doesn’t do the trick, but you can peform the following:

module.log(“some message”)

By default, these messages will appear in syslog or journalctl, prepended with ‘ansible’ for easy grep’ing.

Action Plugins don’t seem to be as easy.

Here’s a work around:

try:
from __main__ import display
except ImportError:
# pylint: disable=ungrouped-imports; this is the standard way how to import
# the default display object in Ansible action plugins.
from ansible.utils.display import Display
display = Display()
...
display.banner("something here")

These will be printed to stdout.

Like what you read? Give Michael Gugino a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.