66

I have a Ansible play for PGBouncer that displays some output from a stats module built into PGBouncer.

My issue is that when Ansible prints the output to the terminal it mangles the newlines. Instead of seeing

----------
| OUTPUT |
----------

I see

----------\n| OUTPUT |\n----------

Does anyone know how to get Ansible to "pretty print" the output?

mjallday
  • 924
  • 2
  • 8
  • 14

7 Answers7

134

If you want more human friendly output define:

ANSIBLE_STDOUT_CALLBACK=debug

This will make ansible use the debug output module (previously named human_log) which despite its unfortunate name is less verbose and much easier to read by humans.

If you get an error that this module is not available, upgrade Ansible or add this module locally if you cannot upgrade ansible, it will work with over versions of ansible like 2.0 or probably even 1.9.

Another option to configure this is to add stdout_callback = debug to your ansible.cfg

Paul Gear
  • 4,367
  • 19
  • 38
sorin
  • 8,016
  • 24
  • 79
  • 103
  • 20
    this should be the *ACCEPTED* answer in 2017 the human friendly log output is shipped out of the box. – Vad1mo Sep 27 '17 at 22:27
  • 2
    Here some more tips to make this more permanent: https://github.com/ansible/ansible/issues/27078#issuecomment-364560173 – kramer65 Apr 10 '18 at 16:55
  • 12
    Or `ANSIBLE_STDOUT_CALLBACK=yaml`. I prefer it because it formats nice `fail` `msg` when I provide an object. – Marinos An Jul 02 '19 at 17:55
19

There isn't a way to do what you want natively in Ansible. You can do this as a workaround:

ansible-playbook ... | sed 's/\\n/\n/g'
jarv
  • 1,303
  • 9
  • 6
  • 1
    On OSX I had to use `sed -e 's/\\n/'$'\\\n/g'`. Also relevant: http://comicjk.com/20 – Navin Jul 01 '17 at 07:29
  • 7
    see sorins answer https://serverfault.com/a/846232/240508 which is the correct one in 2017 and ansible >2.3 – Vad1mo Sep 27 '17 at 22:28
  • Mostly ```\n``` appear in result, so you could use this regexp in your debug message: ```msg: "{{ result.stdout | regex_replace('\\n', '\n') }}"``` – klml Oct 01 '18 at 09:27
  • 4
    unfortunately, doing this removes the nice & useful coloring you get on ansible output – Mehdi LAMRANI Nov 24 '19 at 18:04
19

Found this way in Ansible Project group forum:

- name: "Example test"
  command:
    ...
  register: test
- name: "Example test stdout"
  debug:
    msg: "{{ test.stdout.split('\n') }}"
- name: "Example test stderr"
  debug:
    msg: "{{ test.stderr.split('\n') }}"

We basically turn this into list by splitting it by newline and then printing that list.

jhutar
  • 279
  • 3
  • 7
  • That makes shell output much more legible! Nice! – Asfand Qazi Nov 23 '16 at 11:58
  • This solution appears to have one major drawback - if the execution of the "Example test" module fails, usually the whole playbook build fails and you'll never see the formatted output, especially the one for stderr which is probably most interesting. – René Jul 10 '17 at 13:33
  • @René you are right. For that you can add `ignore_errors: yes` to original command and later something like `- assert: that: "test.rc == 0". – jhutar Jul 10 '17 at 14:01
14

You can use a callback plugin. This will re-parse your output and is easily turned on and off.

xddsg
  • 3,392
  • 2
  • 28
  • 33
  • 2
    Note: With ansible 2.0.x you need to inherit from ``CallbackBase`` imported with ``from ansible.plugins.callback import CallbackBase`` for the callback class to work. – allo May 22 '16 at 20:10
1

If you want to see it in a format that practically mimics standard output, you can use the debug callback plugin with the debug module in Ansible 2.7+ like this:

- name: "Test Output"
  debug:
    msg: "{{ test_result.stdout_lines | join('\n') }}"
0

Despite this is not a concrete response, i've had the same problem within my team and we have implemented ara projet which is an open source software to make ansible human friendly.

You can check here a live demo.

Juan-Kabbali
  • 111
  • 7
0

If you're not running on parallel hosts, you can use the pause module:

- pause:
    prompt: "{{ variable_blob.stdout }}"

Moves on without input by defining minutes or seconds but then user input is not captured.

Credit: https://github.com/ansible/ansible/issues/17446#issuecomment-245391682

Note: On parallel hosts, only output from the first host will be displayed

Ben Moss
  • 3
  • 2