Linux – Ansible task write to local log file

ansibleansible-playbooklinuxshell

Using Ansible I would like to be able to write the sysout of a task running a command to a local(i.e. on the managed server) log file.
For the moment I can only do this using a task like this:

- name: Run my command
  shell: <command> <arg1> <arg3> ... |tee -a <local log file>

The reason to do this is that the takes a long time to complete(i.e. we cannot wait until it finishes to get its output) and would like to collect the output during its execution.

Is there any "Ansible" way to redirect to sysout of the command to a local log file during its execution without using the tee pipe?

Best Answer

You need to use register in the first task and now, you can create a second task to write the output to a local file

- name: shell command
  shell: my_shell_command
  register: myshell_output
- name: copy the output to a local file
  copy:
    content: "{{ myshell_output.stdout }}"
    dest: "/tmp/hello.txt"
  delegate_to: localhost