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


I see

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

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


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'
| improve this answer | |
  • 1
    On OSX I had to use sed -e 's/\\n/'$'\\\n/g'. Also relevant: comicjk.com/20 – Navin Jul 1 '17 at 7:29
  • 6
    see sorins answer serverfault.com/a/846232/240508 which is the correct one in 2017 and ansible >2.3 – Vadimo 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 1 '18 at 9:27
  • 2
    unfortunately, doing this removes the nice & useful coloring you get on ansible output – Mehdi LAMRANI Nov 24 '19 at 18:04

If you want more human friendly output define:


This will make ansible use the debug output module (previously named human_log) whinch 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 probaly even 1.9.

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

| improve this answer | |

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

| improve this answer | |
  • 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

Found this way in Ansible Project group forum:

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

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

| improve this answer | |
  • 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

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

| improve this answer | |

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"
    msg: "{{ test_result.stdout_lines | join('\n') }}"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.