1

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

2
  • You can try to increase Ansible log level: ansible -vvv.
    – Hedi Nasr
    Aug 4 '20 at 7:40
  • -vvvv to enable connection debugging Aug 4 '20 at 7:58
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

2
  • Thank you , will it log even the underlying OS specific commands run in the target machine ?
    – sashank
    Aug 19 '20 at 3:54
  • 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
    Aug 20 '20 at 10:36
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.