Ansible stdout Formatting

What I found to work best so far for getting CLI-like output in Ansible, and which should work out of the box (at least for me on Fedora 34, Ansible 2.9), is setting

stdout_callback = unixy
bin_ansible_callbacks = True

in your ansible.cfg. Given the tasks

  tasks:
    - name: uptime
      shell: uptime
    - name: volumes
      shell: "df -h"

the output in the terminal will look like

- all on hosts: all -
uptime...
  host1 done | stdout:  08:20:09 up 33 min,  1 user,  load average: 0.55, 0.27, 0.26
  host2 done | stdout:  08:20:09 up 1 day,  1:39,  1 user,  load average: 0.18, 0.17, 0.17

volumes...
  host1 done | stdout: Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.2G  1.5G  5.4G  21% /overlay/pivot
devtmpfs        212M     0  212M   0% /dev
none            217M     0  217M   0% /overlay/pivot/overlay
none            217M  137M   80M  64% /overlay/rwdata
overlay         217M  137M   80M  64% /
tmpfs           217M     0  217M   0% /dev/shm
tmpfs           217M   25M  192M  12% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           217M     0  217M   0% /sys/fs/cgroup
/dev/mmcblk0p1  253M   53M  200M  21% /boot
tmpfs            44M     0   44M   0% /run/user/1000
  host2 done | stdout: Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.2G  1.5G  5.4G  22% /overlay/pivot
devtmpfs        212M     0  212M   0% /dev
none            217M     0  217M   0% /overlay/pivot/overlay
none            217M  103M  114M  48% /overlay/rwdata
overlay         217M  103M  114M  48% /
tmpfs           217M     0  217M   0% /dev/shm
tmpfs           217M  5.8M  211M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           217M     0  217M   0% /sys/fs/cgroup
/dev/mmcblk0p1  253M   53M  200M  21% /boot
tmpfs            44M     0   44M   0% /run/user/1000


- Play recap -
  host1           : ok=1    changed=1    unreachable=0    failed=0    rescued=0    ignored=0   
  host2           : ok=1    changed=1    unreachable=0    failed=0    rescued=0    ignored=0   

You should be able to list all available callback plugins using ansible-doc -t callback -l and their respective documentation using ansible-doc -t callback <plugin name>

Source documentation: https://docs.ansible.com/ansible/2.9/plugins/callback.html


Another option:

https://blog.alexgittings.com/improving-the-ansible-output-with-anstomlog/

just store it inside ansible/ansible.cfg

➜ tree ansible     
ansible
├── ansible.cfg
├── callbacks
│   ├── anstomlog.py
└── playbooks
    └── nginx.yaml

ANSIBLE_CONFIG=ansible/ansible.cfg ansible-playbook -u centos --private-key .ssh/key -i `terraform output bastion_ip`, ansible/playbooks/nginx.yaml

enter image description here


Try this option. You’ll love it.

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 format without installing custom plugins.

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 I can easily read through your output message

If you get the following error:

ERROR! Invalid callback for stdout specified: yaml

run

ansible-galaxy collection install community.general