67

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

6 Answers6

142

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,686
sorin
  • 8,454
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,361
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.

jhutar
  • 277
14

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

xddsg
  • 3,540
0

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

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