33

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?

13

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 '17 at 7:29
  • 3
    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
51

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

13

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 '16 at 20:10
11

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.

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

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

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.