# How to correctly print a multiline stdout?

**URL:** <https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416>\
**Category:** Ansible Project\
**Created:** [September 4, 2013, 3:01pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416 "2013-09-04T15:01:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Grilly](https://avatars.discourse-cdn.com/v4/letter/n/77aa72/32.png) [@Nicolas\_Grilly](https://forum.ansible.com/u/Nicolas_Grilly)\
**Post date:** [September 4, 2013, 3:01pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/1 "2013-09-04T15:01:15Z")

</div>

Hello,

We run our database migrations using Ansible with something along these lines:

- name: Run database migrations  
command: env/bin/python migrate.py -q chdir=$project\_dir/releases/$release\_date  
register: result  
changed\_when: result.stdout

migrate.py prints the list of applied migrations to stdout.

We try to print this list because our developers want some feedback about which migrations were applied:

- debug: msg=“{{result.stdout}}”

when: result.stdout

But all new lines are escaped to \n, which makes the output quite difficult to read.

Is there any solution to this?

In your own projects, how do you execute your database migrations? Do you try to get some feedback as what we try to achieve here? Or do you use another approach?

Thanks,

– Nicolas

---

<div class="post-metadata">

**Author:** ![Firat\_Arig](https://avatars.discourse-cdn.com/v4/letter/f/ee7513/32.png) [@Firat\_Arig](https://forum.ansible.com/u/Firat_Arig)\
**Post date:** [September 5, 2013, 11:18am UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/2 "2013-09-05T11:18:31Z")

</div>

Hi there,

Printing lines separately can be done like

- name: Run database migrations  
command: env/bin/python migrate.py -q chdir=$project\_dir/releases/$release\_date  
register: result  
changed\_when: result.stdout\_lines

- debug: msg=“{{item}}”

with\_items: result.stdout\_lines  
when: result.stdout\_lines

Cheers,

– Firat

4 Eylül 2013 Çarşamba 18:01:15 UTC+3 tarihinde Nicolas Grilly yazdı:

---

<div class="post-metadata">

**Author:** ![Nicolas\_Grilly](https://avatars.discourse-cdn.com/v4/letter/n/77aa72/32.png) [@Nicolas\_Grilly](https://forum.ansible.com/u/Nicolas_Grilly)\
**Post date:** [September 6, 2013, 2:29pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/3 "2013-09-06T14:29:39Z")

</div>

Waouh, that’s very clever 🙂

But the output is still a bit “weird” for this use case:

ok: [[host.domain.net](http://host.domain.net)] =\> (item=foo) =\> {“item”: “foo”, “msg”: “foo”}  
ok: [[host.domain.net](http://host.domain.net)] =\> (item=bar) =\> {“item”: “bar”, “msg”: “bar”}  
…

Is there another way?

– Nicolas

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 6, 2013, 9:01pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/4 "2013-09-06T21:01:48Z")

</div>

Easiest way to debug this is just to use “-v” and it will output the previous data.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Grilly1](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nicolas\_Grilly1](https://forum.ansible.com/u/Nicolas_Grilly1)\
**Post date:** [September 6, 2013, 11:59pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/5 "2013-09-06T23:59:07Z")

</div>

Yes, but if result.stdout contains new lines, they will show up escaped as  
\n instead of "real" new lines, which makes it difficult to read. Any  
advice on this?

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [September 7, 2013, 1:29am UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/6 "2013-09-07T01:29:57Z")

</div>

You could try:

debug: msg="{{ print(result.stdout) }}"

The print() function should turn the \n's into new lines.

-jlk

---

<div class="post-metadata">

**Author:** ![Nicolas\_Grilly1](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nicolas\_Grilly1](https://forum.ansible.com/u/Nicolas_Grilly1)\
**Post date:** [September 7, 2013, 11:13am UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/7 "2013-09-07T11:13:34Z")

</div>

It fails with the following error:

TASK: [debug msg="{{print(result.stdout)}}"]

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 7, 2013, 12:47pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/8 "2013-09-07T12:47:27Z")

</div>

Yep, we don’t export random functions here.

“{{ }}” is a call for string templating, it is not a place to insert arbitrary python code.

You wouldn’t want os.unlink() in there either! 🙂

---

<div class="post-metadata">

**Author:** ![Nicolas\_Grilly1](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nicolas\_Grilly1](https://forum.ansible.com/u/Nicolas_Grilly1)\
**Post date:** [September 7, 2013, 1:16pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/9 "2013-09-07T13:16:28Z")

</div>

Hello Michael,

Yes, I know 🙂 It was the purpose of the small pull request James accepted  
yesterday to update the documentation about Jinja2 expressions in playbooks.

This is why I was a quite surprised by Jesse suggestion. I just gave it a  
try to make sure I was not missing something, and hopefully it doesn't work  
🙂

Any idea on how to solve the problem I stated at the start of this  
discussion: We have a playbook that executes database schema migrations,  
and I want to print the list of applied migrations on the console?... The  
list of applied migrations is printed on stdout by the migration command.

Cheers,

Nicolas

---

<div class="post-metadata">

**Author:** ![Firat\_Arig](https://avatars.discourse-cdn.com/v4/letter/f/ee7513/32.png) [@Firat\_Arig](https://forum.ansible.com/u/Firat_Arig)\
**Post date:** [September 9, 2013, 6:29am UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/10 "2013-09-09T06:29:38Z")

</div>

I don’t think what you want it is currently possible through ansible. Polling the output of a file would be a less hacky way to address your problem.

7 Eylül 2013 Cumartesi 16:16:28 UTC+3 tarihinde Nicolas Grilly yazdı:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 9, 2013, 2:46pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/11 "2013-09-09T14:46:44Z")

</div>

It seems what we really want is a better way for the debug module to work with structured and multiline data, like:

debug: var=foo.stdout\_lines

---

<div class="post-metadata">

**Author:** ![Jason\_Freeman](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@Jason\_Freeman](https://forum.ansible.com/u/Jason_Freeman)\
**Post date:** [September 10, 2013, 9:06pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/12 "2013-09-10T21:06:39Z")

</div>

If you are really looking for specialized output, why bother using debug module? Add the following task and create some simple script to parse. The inventory\_hostname will give the remote host corresponding to the stdout (you don’t want to mix those up).

- local\_action: command bash /my/local/path/writer.sh “$inventory\_hostname” “{{ item }}”  
with\_items: result.stdout\_lines  
when: result.stdout

A simple bash script on your local machine (writer.sh)…

#!/bin/sh  
echo $1 $2 \>\> out.log

I came looking for something similar, but there doesn’t seem to be a great way to format output (built in).

hope it helps

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 11, 2013, 12:24pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/13 "2013-09-11T12:24:34Z")

</div>

Probably because it requires writing bash in the middle of your Ansible playbook, which is something we want to avoid people having to do.

It would be much better to enhance the debug module as it would make the playbook more readable without resorting to hijinks.

---

<div class="post-metadata">

**Author:** ![Brad\_Smith](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@Brad\_Smith](https://forum.ansible.com/u/Brad_Smith)\
**Post date:** [May 5, 2015, 5:16pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/14 "2015-05-05T17:16:58Z")

</div>

Did anything ever come of this? I would really like to be able to print friendly “Provisioning succeeded! Here’s what to do next…” messages at the end of some of my playbooks.

–Brad

---

<div class="post-metadata">

**Author:** ![Nicolas\_Grilly](https://avatars.discourse-cdn.com/v4/letter/n/77aa72/32.png) [@Nicolas\_Grilly](https://forum.ansible.com/u/Nicolas_Grilly)\
**Post date:** [May 5, 2015, 7:17pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/15 "2015-05-05T19:17:34Z")

</div>

Not to my knowledge. I’d like to have a solution 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 5, 2015, 7:45pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/16 "2015-05-05T19:45:53Z")

</div>

everything that has stdout, shoudl have stdout\_lines, which is already  
multiline, for those that don't just use split('/n').

---

<div class="post-metadata">

**Author:** ![Zhendong\_Zhao](https://avatars.discourse-cdn.com/v4/letter/z/3ec8ea/32.png) [@Zhendong\_Zhao](https://forum.ansible.com/u/Zhendong_Zhao)\
**Post date:** [July 8, 2022, 4:03pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/17 "2022-07-08T16:03:08Z")

</div>

Hi @Nicolas Grilly,  
I met the same issue… It’s year 2022 already, did you get any solution? thanks.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Grilly](https://avatars.discourse-cdn.com/v4/letter/n/77aa72/32.png) [@Nicolas\_Grilly](https://forum.ansible.com/u/Nicolas_Grilly)\
**Post date:** [July 9, 2022, 10:20am UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/18 "2022-07-09T10:20:57Z")

</div>

Nope, no solution.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [July 9, 2022, 10:34am UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/19 "2022-07-09T10:34:03Z")

</div>

Hi,

there are multiple solutions, even if there's no perfect one that  
works out of the box:

1. Use `debug: msg="{{ result.stdout_lines }}"`. Not perfect, but  
&nbsp;&nbsp;&nbsp;a lot better to read than `debug: msg="{{ result.stdout }}"`.

2. Check out the many callback plugins that exist:  
&nbsp;&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/collections/index\_callback.html](https://docs.ansible.com/ansible/latest/collections/index_callback.html)  
&nbsp;&nbsp;&nbsp;Maybe one of them supports this? If not, you could write your own.  
&nbsp;&nbsp;&nbsp;(I would try  
&nbsp;&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/collections/community/general/diy\_callback.html](https://docs.ansible.com/ansible/latest/collections/community/general/diy_callback.html)  
&nbsp;&nbsp;&nbsp;- it seems to be very configurable, maybe it can do this?)

3. You could write an action plugin similar to `debug` that prints the  
&nbsp;&nbsp;&nbsp;given parameter without any quoting.

&nbsp;&nbsp;&nbsp;(If someone wants to contribute something like that to  
&nbsp;&nbsp;&nbsp;community.general, I'm happy to review it.)

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 9, 2022, 2:45pm UTC](https://forum.ansible.com/t/how-to-correctly-print-a-multiline-stdout/13416/20 "2022-07-09T14:45:31Z")

</div>

Try \*yaml\* callback  
[https://docs.ansible.com/ansible/latest/collections/community/general/yaml\_callback.html](https://docs.ansible.com/ansible/latest/collections/community/general/yaml_callback.html)

For example, the playbook

&nbsp;&nbsp;\> cat pb.yml  
&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stdout: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line3  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result.stdout

gives (abridged)

&nbsp;&nbsp;\> ANSIBLE\_STDOUT\_CALLBACK=yaml ansible-playbook pb.yml

&nbsp;&nbsp;ok: [localhost] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;result.stdout: |-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line3
