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:

...it'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 .

您可能感兴趣的

Ansible + Windows Webinar Q&A The Ansible Ask an Expert webinar series continues to be one of the most popular series we’ve ever hosted. During these Q&A style webinars,...
Ansible 学习指北 因为工作的缘故接触并积极推动Ansible在企业级生产环境的落地,独立承担并实现了《基于ansible的主机自动化配置管理》项目,此前也先后接触过Puppet和SaltStack,本文不会讨论开源或者自主研发方案的优劣,重点是和大伙儿分享自己在ansible技术领域积累的一些项目实战经验,如果...
Automatically Deploying ClipBucket with Ansible Overview ClipBucket is an open source video hosting platform, similar in functionality to YouTube or Vimeo. In this guide, we’ll walk through how t...
Sample Network Automation Ansible Playbooks I developed over a dozen different Ansible-based network automation solutions in the last two years for my network automation workshops and online cou...
Top 5: A powerful sysadmin tool, deploying serverl... Top 5: A powerful sysadmin tool, deploying serverless apps with Ansible, and more We round up five of the week's most popular articles. ...