Ansible task write to local log file
39,037
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
Related videos on Youtube
Author by
Abdul
Updated on September 18, 2022Comments
-
Abdul over 1 year
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? -
Abdul about 5 yearsThis could be a solution, but I would prefer to write to the log file during the my_shell_command execution not get the whole sysout after the execution completes.
-
c4f4t0r about 5 years@trikelef ansible is not shell, that you do command | tee and so on, you need to change this logic, unless you will start to using ansible as wrapper
-
Henrik Pingel about 4 yearsI believe the shell command is executed on the remote host so you won't be able to pipe the command output to a local file like that. The
local log file
in your example will be on the remote host. -
Matt about 4 yearsThis does not answer OP's question - is there an Ansible way to do this?
-
Michael Hampton over 3 yearsThis suggests that the ansible user's shell isn't
/bin/bash
but something else. -
Adam J Richardson about 2 yearsOP asked about Ansible. This solution has no Ansible in it.