Use Ansible’s YAML callback plugin for a better CLI experience

综合技术 2018-04-20 阅读原文

Ansible is a great tool for automating IT workflows, and I use it to manage hundreds of servers and cloud services on a daily basis. One of my small annoyances with Ansible, though, is it's default CLI output—whenever there's a command that fails, or a command or task that succeeds and dumps a bunch of output to the CLI, the default visible output is not very human-friendly.

For example, in a Django installation example from chapter 3 of my book Ansible for DevOps , there's an ad-hoc command to install Django on a number of CentOS app servers using Ansible's yum module. Here's how it looks in the terminal when you run that task the first time, using Ansible's default display options, and there's a failure:'s not quickly digestible—and this is one of the shorter error messages I've seen!

Ansible introduced callback plugins a while ago, but I just noticed there's a new YAML callback plugin introduced with Ansible 2.5—meaning any machine running Ansible 2.5.0 or later can automatically start using this wonderfully-optimized-for-humans format, without you needing to install a custom plugin on the machine, or include it with all your projects!

To use it, edit your ansible.cfg file (either global, in /etc/ansible/ansible.cfg , or a local one in your playbook/project), and add the following lines under the [defaults] section:

# Use the YAML callback plugin.
stdout_callback = yaml
# Use the stdout_callback when running ad-hoc commands.
bin_ansible_callbacks = True

Now, let's try the same Ansible command from earlier, this time using the YAML callback plugin:

Ah, much better! Now I can easily read through the error message, just as if it was streamed from the server into my terminal. And if you need to parse the data, it's valid YAML, so it's just as easy as the JSON you'd get previously. If you're looking for a usable CLI experience, there are a few other good built-in callback plugins you might want to try, too, like unixy , dense , or debug .

If you want to go even deeper, and maybe even write your own callback plugins, check out this presentation from AnsibleFest San Francisco 2017: The Power of Callback Plugins .

Jeff Geerling's Blog

责编内容by:Jeff Geerling's Blog阅读原文】。感谢您的支持!