Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Control ansible verbosity and silence with tags

I wonder if I have stumbled upon a use case that other haven't yet. I just learned about how to use callback plugins in ansible, which are generally responsible for output. I found one named "selective" which means that it will show the output for only the tasks with a tag titled "print_action." My goals included having mundane tasks be silent, not whitelisting the important tasks. Plus, it shows every task anyway if you do a -v for verbose. Also, using callbacks took me some effort. To even allow different callback plugins, you have to add the names you want available to the [defaults] callback_whitelist variable in ansible.cfg, which is comma- space-delimited. And place the plugin in of the directories specified by callback_plugins , normally /usr/share/ansible_plugins/callback_plugins. Next, to actually use it for a play, define ANSIBLE_STDOUT_CALLBACK with the name of the plugin to use. So:

$ cat ansible.cfg
[default]
... OUTPUT TRUNCATED ...
callback_whitelist = short
$ ANSIBLE_STDOUT_CALLBACK=short ansible-playbook myplaybook.yml

I present to you today my customized callback plugin! It operates just like the default callback, but with two modifications, both dependent on tags of the tasks.

  • tag: verbose will cause the output to always be like the -v flag
  • tag: silent will completely hide the task output when successful. Failures and skips appear like normal.

My plays tend to have a lot of minor tasks like deploying assistant scripts or cleaning up temp files, and I don't want tons of scrollback in my terminal just because 5 different temp files were removed (or not) at the end of a play across 150 systems.

Comments