How do I get logs/details of ansible-playbook module executions?

79
January 31, 2018, at 5:52 PM

Say I execute the following.

$ cat test.sh
#!/bin/bash
echo Hello World
exit 0
$ cat Hello.yml
---
- hosts: MyTestHost
  tasks:
  - name: Hello yourself
    script: test.sh

$ ansible-playbook  Hello.yml
PLAY [MyTestHost] ****************************************************************
GATHERING FACTS ***************************************************************
ok: [MyTestHost]
TASK: [Hello yourself] ********************************************************
ok: [MyTestHost]
PLAY RECAP ********************************************************************
MyTestHost                    : ok=2    changed=0    unreachable=0    failed=0
$

I know for sure that it was successful.

Where/how do I see the "Hello World" echo'ed/printed by my script on the remote host (MyTestHost)? Or the return/exit code of script?

My research shows me it would be possible to write a plugin to intercept module execution callbacks or something on those lines and write a log file. I would prefer to not waste my time with that.

E.g. something like the stdout in below (note that I'm running ansible and not ansible-playbook):

$ ansible plabb54 -i /project/plab/svn/plab-maintenance/ansible/plab_hosts.txt -m script -a ./test.sh
plabb54 | success >> {
    "rc": 0,
    "stderr": "",
    "stdout": "Hello World\n"
}
$
Answer 1

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 ran ansible-playbook
  • ~/.ansible.cfg
  • /etc/ansible/ansible.cfg
Answer 2

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`."
}
Answer 3

Using callback plugins, you can have the stdout of your commands output in readable form with the play: gist: human_log.py

Edit for example output:

 _____________________________________
< TASK: common | install apt packages >
 -------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

changed: [10.76.71.167] => (item=htop,vim-tiny,curl,git,unzip,update-motd,ssh-askpass,gcc,python-dev,libxml2,libxml2-dev,libxslt-dev,python-lxml,python-pip)
stdout:
Reading package lists...
Building dependency tree...
Reading state information...
libxslt1-dev is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 24 not upgraded.

stderr:
start:
2015-03-27 17:12:22.132237
end:
2015-03-27 17:12:22.136859
READ ALSO
Issues with matshow

Issues with matshow

I am trying to show a matrix and a related vector data together using matplotlib matshow

61
Run code on every folder I have in the directory in a loop

Run code on every folder I have in the directory in a loop

First, I would like to extract fasta sequences from a multi-fasta file by specifying the >header in a list (ie

70
How to mock global variable of List Type?

How to mock global variable of List Type?

I'm trying to mock a global variable which is list type

73
How to write a line to text file when particular condition is satisfied

How to write a line to text file when particular condition is satisfied

I have a loop and checking one condition as follows

59