How do I get logs/details of ansible-playbook module executions?
The playbook script task will generate stdout
just like the non-playbook command, it just needs to be saved to a variable using register
. Once we've got that, the debug module can print to the playbook output stream.
tasks:
- name: Hello yourself
script: test.sh
register: hello
- name: Debug hello
debug: var=hello
- name: Debug hello.stdout as part of a string
debug: "msg=The script's stdout was `{{ hello.stdout }}`."
Output should look something like this:
TASK: [Hello yourself] ********************************************************
changed: [MyTestHost]
TASK: [Debug hello] ***********************************************************
ok: [MyTestHost] => {
"hello": {
"changed": true,
"invocation": {
"module_args": "test.sh",
"module_name": "script"
},
"rc": 0,
"stderr": "",
"stdout": "Hello World\r\n",
"stdout_lines": [
"Hello World"
]
}
}
TASK: [Debug hello.stdout as part of a string] ********************************
ok: [MyTestHost] => {
"msg": "The script's stdout was `Hello World\r\n`."
}
There is also other way to generate log file.
Before running ansible-playbook
run the following commands to enable logging:
Specify the location for the log file.
export ANSIBLE_LOG_PATH=~/ansible.log
Enable Debug
export ANSIBLE_DEBUG=True
To check that generated log file.
less $ANSIBLE_LOG_PATH
Offical plugins
You can use the output callback plugins. For example, starting in Ansible 2.4, you can use the debug output callback plugin:
# In ansible.cfg:
[defaults]
stdout_callback = debug
(Altervatively, run export ANSIBLE_STDOUT_CALLBACK=debug
before running your playbook)
Important: you must run ansible-playbook
with the -v
(--verbose
) option to see the effect. With stdout_callback = debug
set, the output should now look something like this:
TASK [Say Hello] ********************************
changed: [192.168.1.2] => {
"changed": true,
"rc": 0
}
STDOUT:
Hello!
STDERR:
Shared connection to 192.168.1.2 closed.
There are other modules besides the debug
module if you want the output to be formatted differently. There's json
, yaml
, unixy
, dense
, minimal
, etc. (full list).
For example, with stdout_callback = yaml
, the output will look something like this:
TASK [Say Hello] **********************************
changed: [192.168.1.2] => changed=true
rc: 0
stderr: |-
Shared connection to 192.168.1.2 closed.
stderr_lines:
- Shared connection to 192.168.1.2 closed.
stdout: |2-
Hello!
stdout_lines: <omitted>
Third-party plugins
If none of the official plugins are satisfactory, you can try the human_log
plugin. There are a few versions:
https://github.com/n0ts/ansible-human_log
https://gist.github.com/cliffano/9868180
If you pass the -v
flag to ansible-playbook on the command line, you'll see the stdout and stderr for each task executed:
$ ansible-playbook -v playbook.yaml
Ansible also has built-in support for logging. Add the following lines to your ansible configuration file:
[defaults]
log_path=/path/to/logfile
Ansible will look in several places for the config file:
ansible.cfg
in the current directory where you ranansible-playbook
~/.ansible.cfg
/etc/ansible/ansible.cfg