271

How do I see stdout for ansible-playbook commands? -v only shows ansible output, not the individual commands. It would be great if I could figure out how to do this immediately, so if something fails or hangs I can see why.

e.g.

- name: print to stdout
  action: command echo "hello"

would print

TASK: [print variable] ******************************************************** 

hello
shgnInc
  • 1,996
QuinnBaetz
  • 2,819

6 Answers6

256

I think you can register the result to a variable, then print with debug.

- name: print to stdout
  command: echo "hello"
  register: hello

- debug: msg="{{ hello.stdout }}"

- debug: msg="{{ hello.stderr }}"
bfschott
  • 2,669
167

Instead of stdout I would suggest using stdout_lines. For multiline output this is much nicer, e.g.

- hosts: all
  tasks:
    - name: Run ls.sh and output "ls /"
      script: ls.sh
      register: out

    - debug: var=out.stdout_lines

gives

TASK: [debug var=out.stdout_lines] ******************************************** 
ok: [local] => {
    "var": {
        "out.stdout_lines": [
            "total 61", 
            "lrwxrwxrwx   1 root root     7 Feb 15  2015 bin -> usr/bin", 
            "drwxr-xr-x   6 root root  1024 Aug 24 22:08 boot", 
            "drwxr-xr-x  22 root root  3580 Sep  8 18:41 dev",  
            [...] 
            "drwxr-xr-x   9 root root  4096 Aug 25 19:14 usr", 
            "drwxr-xr-x  13 root root  4096 Feb 25  2015 var"
        ]
    }
}

Regarding real time output for debugging purposes there is a closed bug report https://github.com/ansible/ansible/issues/3887#issuecomment-54672569 discussing the reasons why this is not possible and will not be implemented.

Mars
  • 1,801
  • 1
  • 11
  • 2
33

I found using the minimal stdout_callback with ansible-playbook gave similar output to using ad-hoc ansible.

In your ansible.cfg (Note that I'm on OS X so modify the callback_plugins path to suit your install)

stdout_callback     = minimal
callback_plugins    = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ansible/plugins/callback

So that a task such as this

---
- hosts: example
  tasks:
   - name: Say hi
     command: echo "hi ..."

Gives output like this, like an ad-hoc command would

example | SUCCESS | rc=0 >>
hi ...

I'm using ansible-playbook 2.2.1.0

Jason S
  • 463
11

If you really want to watch the output in realtime, there is a hacky way around it, at least for the ansible shell module.

In whatever shell script wraps your call to ansible, touch and tail a log file in a background job. Then redirect the ansible shell command's output to append to that log file. You need to make sure you kill the background tail job after ansible finishes, or it will be left dangling.

For example, in a bash script that calls ansible:

set -m
touch /tmp/debug.log && tail -f /tmp/debug.log &
ansible-playbook ... call playbook here
kill %1   # ensure the background tail job is stopped

Then in some ansible role:

- name: Run a script and print stdout/stderr
  shell: bash -c "/run/something.sh 2>&1 >> /tmp/debug.log"
cobbzilla
  • 301
3

I've found that just killing a stuck process on the remote via ssh gives back the stdout. I find thats shorter than writing workarounds that won't be used in the final playbook anyways:

kill -9 PID

dza
  • 172
1

Another way to get stdout of a command is to add more verbosity with -vvv. You don't have to write debug tasks, and it gives you good and viewable (JSON on multiple lines) output for everything. The only downside is that it speaks a lot (ssh log in, privilege escalation, ...).

tholeb
  • 59