Ansible internally uses SSH Commands to run on the target machine . Is there a way to log the commands that are being executed ?
2 Answers
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 thelog_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
-
Thank you , will it log even the underlying OS specific commands run in the target machine ?– sashankCommented Aug 19, 2020 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.– bgdnlpCommented Aug 20, 2020 at 10:36
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
ansible -vvv
.-vvvv to enable connection debugging