# Debug print list nicely

**URL:** <https://forum.ansible.com/t/debug-print-list-nicely/31545>\
**Category:** Ansible Project\
**Created:** [October 25, 2019, 10:18am UTC](https://forum.ansible.com/t/debug-print-list-nicely/31545 "2019-10-25T10:18:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Iuga](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Christian\_Iuga](https://forum.ansible.com/u/Christian_Iuga)\
**Post date:** [October 25, 2019, 10:18am UTC](https://forum.ansible.com/t/debug-print-list-nicely/31545/1 "2019-10-25T10:18:47Z")

</div>

maybe a stupid question, but how to print nicely a list

Ideally, i would like to print :

- all
- first
- second

But i’m not able to do it, please see 4 examples made :

- hosts: localhost  
gather\_facts: no  
vars:  
found:

- all

- first

- second  
tasks:

- name: test1  
debug:  
var: found

- name: test2  
debug:  
var: item  
loop: “{{ found }}”

- name: test3  
debug:  
msg: “{{ found | to\_nice\_yaml }}”

- name: test4  
debug:  
msg: “{{ found | to\_nice\_yaml | indent(2, False) }}”

> Result :

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [test1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“found”: [  
“all”,  
“first”,  
“second”  
]  
}

TASK [test2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=all) =\> {  
“ansible\_loop\_var”: “item”,  
“item”: “all”  
}  
ok: [localhost] =\> (item=first) =\> {  
“ansible\_loop\_var”: “item”,  
“item”: “first”  
}  
ok: [localhost] =\> (item=second) =\> {  
“ansible\_loop\_var”: “item”,  
“item”: “second”  
}

TASK [test3] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “- all\n- first\n- second\n”  
}

TASK [test4] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “- all\n - first\n - second\n”  
}

The test3 is the nearest solution but ‘\n’ is not interpreted by the debug …

---

<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:** [October 25, 2019, 10:57am UTC](https://forum.ansible.com/t/debug-print-list-nicely/31545/2 "2019-10-25T10:57:04Z")

</div>

It's possible to \*split\* the lines. For example

&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ msg.split('\n') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ found | to\_nice\_yaml }}"

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Christian\_Iuga](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Christian\_Iuga](https://forum.ansible.com/u/Christian_Iuga)\
**Post date:** [October 25, 2019, 11:15am UTC](https://forum.ansible.com/t/debug-print-list-nicely/31545/3 "2019-10-25T11:15:04Z")

</div>

Tkx vlado for the tips

But it’s not 'perfect : it’s create a empty line :

ok: [localhost] =\> {  
“msg”: [  
“- all”,  
“- first”,  
“- second”,  
“”  
]  
}

---

<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:** [October 25, 2019, 11:51am UTC](https://forum.ansible.com/t/debug-print-list-nicely/31545/4 "2019-10-25T11:51:18Z")

</div>

Empty line has been created because of the trailing '\n'. Get rid of it if  
you want to.

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ msg[:-1].split('\n') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ found | to\_nice\_yaml }}"

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Uriel](https://avatars.discourse-cdn.com/v4/letter/u/53a042/32.png) [@Uriel](https://forum.ansible.com/u/Uriel)\
**Post date:** [October 25, 2019, 8:19pm UTC](https://forum.ansible.com/t/debug-print-list-nicely/31545/5 "2019-10-25T20:19:03Z")

</div>

One alternative to trying to format things in the task - you can set the stdout\_callback value to yaml in ansible.cfg. It will take your tasks and make it more human-readable.

Got the tip from geerlingguy’s blog: [https://jeffgeerling.com/blog/2018/use-ansibles-yaml-callback-plugin-better-cli-experience](https://www.jeffgeerling.com/blog/2018/use-ansibles-yaml-callback-plugin-better-cli-experience)

---

<div class="post-metadata">

**Author:** ![sdoran](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@sdoran](https://forum.ansible.com/u/sdoran)\
**Post date:** [October 30, 2019, 1:49pm UTC](https://forum.ansible.com/t/debug-print-list-nicely/31545/6 "2019-10-30T13:49:23Z")

</div>

I prefer to set the stdout callback plugin to debug, though yaml is also pretty good. That should make the output more readable.

In the config:  
[defaults]  
stdout\_callback = debug

As an environment variable:  
ANSIBLE\_STDOUT\_CALLBACK=debug
