How to send Ansible debug messages to another file?
Without writing any task(s), re-implementing functionality which is already there, just by configuration and whitelisting an additional Callback plugin, see Index of all Callback Plugins, namely log_plays callback – write playbook output to log file.
With an ansible.cfg
[default]
stdout_callback = yaml
callback_whitelist = timer, profile_tasks, log_plays
[callback_log_plays]
log_folder = /tmp/ansible/hosts
a minimal example playbook
---
- hosts: test
become: false
gather_facts: false
tasks:
will result into an stdout of
PLAY [test] *********************************************************************************************
Saturday 14 October 2023 12:45:15 +0200 (0:00:00.041) 0:00:00.041 ******
TASK [debug] ********************************************************************************************
ok: [test.example.com] =>
msg: Hello world!
PLAY RECAP **********************************************************************************************
test.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Playbook run took 0 days, 0 hours, 0 minutes, 0 seconds
Saturday 14 October 2023 12:45:15 +0200 (0:00:00.068) 0:00:00.109 ******
===============================================================================
and a log file
~/test$ ll /tmp/ansible/hosts/
total 52
-rw-r--r--. 1 ansible_user users 314 Oct 14 12:45 test.example.com
with content
~/test$ cat /tmp/ansible/hosts/test.example.com
Oct 14 2023 12:45:15 - log_plays.yml - - debug - OK - {"msg": "Hello world!", "changed": false, "_ansible_verbose_always": true, "_ansible_no_log": false}
Similar and further Q&A
and regarding the log_plays callback usage