62

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?

0

7 Answers 7

18

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'
4
  • 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
  • 3
    unfortunately, doing this removes the nice & useful coloring you get on ansible output Nov 24, 2019 at 18:04
128

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) 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

3
  • 20
    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
  • 2
    Here some more tips to make this more permanent: github.com/ansible/ansible/issues/27078#issuecomment-364560173
    – kramer65
    Apr 10, 2018 at 16:55
  • 11
    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
18

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.

3
  • 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
13

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

1
  • 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
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

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

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.

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.