Ansible persistent logging

Basecamp

By default, Ansible logs the output of playbooks to stdout. This is all fun and games until you close the terminal, something breaks, your logs are gone and you (like me) don’t remember exactly what you did.

Luckily, persistent logging can easily be configured on the Ansible controller. These logs do not give you the same degree of insight into each task as aap/awx, but at least you know when, where, and what happened.

Persistent logging is enabled in ansible.cfg by leveraging the community.general.log_plays callback.

$ cat ansible.cfg

[defaults]
callbacks_enabled = community.general.log_plays

[callback_log_plays]
log_folder = /Users/jornivar/.ansible/log/hosts

By default, logs are stored per host in /var/log/ansible/hosts, but I usually change this to ~/.ansible/log/hosts. Here is an example of some log output.

$ cat ~/.ansible/log/hosts/localhost

Jul 22 2024 13:23:59 - test_playbook.yml - Print hello. - ansible.builtin.debug - OK - {"msg": "hello", "_ansible_verbose_always": true, "_ansible_no_log": false, "changed": false}

Jul 22 2024 13:26:29 - test_playbook.yml - Print hello. - ansible.builtin.debug - OK - {"msg": "hello", "_ansible_verbose_always": true, "_ansible_no_log": false, "changed": false}

I don’t use these logs a lot, but it is nice to know that they are there should they ever be needed.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *