6

If you do the following:

- name: print to stdout
  command: echo "My log information"
  register: logdata

- debug: msg="{{ logdata.stdout }}"

The logdata register variable's contents will be displayed along with the complete ansible log. I would like these debug messages to be stored in another file. Is this possible by any means?

The other alternative I thought of was to have an array of register variables. And finally use the copy module to put the content of that array to a file. Not the best option. If it is possible to redirect the stdout of debug to another file, it would be cool.

0aslam0
  • 169

3 Answers3

6

Use local_action as under after capturing the output in a variable:

- local_action: 
        module: copy 
        content: "{{ variable1 }}"
        dest: /tmp/whatever.out
womble
  • 98,245
1

You can use the lineinfile module?

- lineinfile: create=yes regexp="NONEXISTENTLINE" dest=/tmp/ansible.log line="{{logdata.stdout}}" state=present

I use regexp="NONEXISTENTLINE" to allow the same message to be logged several times. Maybe you don't need that.

The log will be on each target host. Pull it or not.

Alternatively, you can parse the output with awk or something.

yarl
  • 208
0

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:

  • debug:

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

U880D
  • 1,275