4

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
  • 141
  • 1
  • 1
  • 6

2 Answers2

4

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

- local_action: 
        module: copy 
        content: "{{ variable1 }}"
        dest: /tmp/whatever.out
womble
  • 95,029
  • 29
  • 173
  • 228
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
  • 192
  • 1
  • 6