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阅读原文】。感谢您的支持!


Kubernetes v1.7 新特性解析:CustomResourceDefinition... CustomResourceDefinition(CRD)是v1.7+新增的无需改变代码就可以扩展Kubernetes API的机制,用来管理自定义对象。它实...
Ansible 详解(四):Ansible-vault 和 Python api... ansible 是一款轻量级自动化运维工具,由的 Python 语言开发,结合了多种自动化运维工具的特性,实现了批量系统配置,批量程序部署,批量命令执行等功能;...
Ansible 运维自动化 编辑推荐: 本文来源个人微博...
ansible模块authorized_key 示例:# 新增公钥内容到服务器用户家目录的.ssh目录的authorized_keys文件 没有则创建authorized_ke...