<p>Set the log file to use inline or <a href="https://docs.ansible.com/ansible/latest/reference_appendices/logging.html" rel="nofollow noreferrer">in Ansible config</a>, then run ansible-playbook with verbose option:</p>
<pre><code>ANSIBLE_LOG_PATH=&quot;ansible-$(date +%Y%m%d%H%M%S).log&quot; \
ansible-playbook -vvvv \
    --diff \
    -i inventory/myinventory \
    myplaybook.yml
</code></pre>
<ul>
<li><code>ANSIBLE_LOG_PATH</code> - is an environment variable that <a href="https://docs.ansible.com/ansible/latest/reference_appendices/config.html#envvar-ANSIBLE_LOG_PATH" rel="nofollow noreferrer">maps</a> to the <code>log_path</code> configuration item. Sets the file to log to. When using an environment variable you can use shell substitution to add the date and time to the file name so the next run doesn't overwrite the log file.</li>
<li><code>-vvvv</code> - be very very very verbose. Will show SSH commands executed.</li>
<li><code>--diff</code> - bonus, when making a change show what has been done.</li>
<li>the rest are standard options.</li>
</ul>
<p>The log will be in a file in the current directory named something like <code>ansible-20200818155322.log</code></p>

CC BY-SA 4.0
2020-08-19T03:54:50.787Z
Thank you , will it log even the underlying OS specific commands run in the target machine ?
sashank
CC BY-SA 4.0
2020-08-20T10:36:43.097Z
It logs the exact SSH command execute, which also has the command run on the target machine. However, sometimes the command run on the machine is to execute an Ansible script which does the actual work. So, yes, it will log the executed command, but that might not always be what you are looking for.
bgdnlp
CC BY-SA 4.0
2020-08-18T15:53:09.930Z
2020-08-18T15:53:09.930Z
bgdnlp
<p>Use the register module and store the result in a variable.</p>
<pre><code> - hosts: all
   tasks:
     - name: Ansible register variable basic example
       shell: &quot;find *.txt&quot;
       args:
         chdir: &quot;/Users/mdtutorials2/Documents/Ansible&quot;
       register: find_output

     - debug:
         var: find_output
</code></pre>

CC BY-SA 4.0
2020-08-18T14:13:59.990Z
2020-08-18T14:13:59.990Z
Shaha Nawaj Mulla
<p>Ansible internally uses SSH Commands to run on the target machine . Is there a way to log the commands that are being executed ?</p>

CC BY-SA 4.0
2020-08-04T07:40:23.330Z
You can try to increase Ansible log level: `ansible -vvv`.
Hedi Nasr
CC BY-SA 4.0
2020-08-04T07:58:41.153Z
`-vvvv to enable connection debugging`
Vladimir Botka
<ansible>
How to log native commands run by Ansible Playbook
sashank
# How to log native commands run by Ansible Playbook

(Asked by: username_0 on 2020-08-04)

Ansible internally uses SSH Commands to run on the target machine . Is there a way to log the commands that are being executed ?

**Tags:** ansible

> **Comments:**
> **username_1**: You can try to increase Ansible log level: `ansible -vvv`.  
> **username_2**: `-vvvv to enable connection debugging`  

---

## 2 answers

### Answer by username_3 on 2020-08-18. Score: 2

Set the log file to use inline or in Ansible config, then run ansible-playbook with verbose option:
ANSIBLE_LOG_PATH="ansible-$(date +%Y%m%d%H%M%S).log" \
ansible-playbook -vvvv \
    --diff \
    -i inventory/myinventory \
    myplaybook.yml


ANSIBLE_LOG_PATH - is an environment variable that maps to the log_path configuration item. Sets the file to log to. When using an environment variable you can use shell substitution to add the date and time to the file name so the next run doesn't overwrite the log file.
-vvvv - be very very very verbose. Will show SSH commands executed.
--diff - bonus, when making a change show what has been done.
the rest are standard options.

The log will be in a file in the current directory named something like ansible-20200818155322.log

> **Comments:**
> **username_0**: Thank you , will it log even the underlying OS specific commands run in the target machine ?  
> **username_3**: It logs the exact SSH command execute, which also has the command run on the target machine. However, sometimes the command run on the machine is to execute an Ansible script which does the actual work. So, yes, it will log the executed command, but that might not always be what you are looking for.  

---

### Answer by username_4 on 2020-08-18. Score: 0

Use the register module and store the result in a variable.
 - hosts: all
   tasks:
     - name: Ansible register variable basic example
       shell: "find *.txt"
       args:
         chdir: "/Users/mdtutorials2/Documents/Ansible"
       register: find_output

     - debug:
         var: find_output


---

