How to send Ansible debug messages to another file

ansibledebuglogging

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.

Best Answer

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

- local_action: 
        module: copy 
        content: "{{ variable1 }}"
        dest: /tmp/whatever.out