Ansible - pretty print output

We're having 2024 and writing about ansible might seem a little dated because terraform is fairly dominant nowadays. And it's true, terraform is meant to work with APIs [and really good in this domain]. The reality is, not everything has an API 😀

One task where i'm working currently with ansible is to create some CICD (gitlab) pipelines that need to either build applications or deploy infrastructure and might even manage dedicated systems or networking equipment. As i'm really not into bash and scripting (due too much state management, typically poor error handling due lack of time, ...) ansible is the way to go.

One simple requirement is: Output should be shown human readable in the logs so that some diffs (or even terraform plans) can be verified before changes go live.

unformatted output

This looks not very nice. I would guess that no one (except neo from the matrix) can read this.

YAML format to the rescue

The solution is fairly easy. You can get ansible to make a pretty print version of the output, this looks like this:

yaml formatted output

How to get this you ask?

Configure using ENV

To change this on the fly, use an ENV

ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook myplay.yml

output format using env

You can also persist this in the ansible.cfg

[defaults]
...
# set the output plugin
stdout_callback = yaml

[inventory]
enable_plugins = yaml

configuration in ansible.cfg

That's it - quick & simple.