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?


6 Answers 6


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

If you want more human friendly output define:


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

  • 23
    this should be the ACCEPTED answer in 2017 the human friendly log output is shipped out of the box.
    – Vad1mo
    Sep 27, 2017 at 22:27
  • 3
    Here some more tips to make this more permanent: github.com/ansible/ansible/issues/27078#issuecomment-364560173
    – kramer65
    Apr 10, 2018 at 16:55
  • 14
    Or ANSIBLE_STDOUT_CALLBACK=yaml. I prefer it because it formats nice fail msg when I provide an object.
    – Marinos An
    Jul 2, 2019 at 17:55

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.

  • That makes shell output much more legible! Nice! Nov 23, 2016 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, 2017 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, 2017 at 14:01

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

  • 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, 2016 at 20:10

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') }}"

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .