# template for rows in a table

**URL:** <https://forum.ansible.com/t/template-for-rows-in-a-table/38537>\
**Category:** Ansible Project\
**Created:** [October 20, 2023, 11:41am UTC](https://forum.ansible.com/t/template-for-rows-in-a-table/38537 "2023-10-20T11:41:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [October 20, 2023, 11:41am UTC](https://forum.ansible.com/t/template-for-rows-in-a-table/38537/1 "2023-10-20T11:41:23Z")

</div>

Hi,

Looking for a template which prints the list’s items in the horizontal (rows) of a tables.  
I have the below list and trying to print as a table.

vars:  
color:

- green
- red
- yellow
- blue  
cities:
- toronto
- montreal
- mumbai
- dubai  
lang:
- English
- French
- Hindi
- Arabic

and a desired out as below

color cities lang  
green toronto English  
red montreal French  
yellow mumbai hindi  
blue dubai bangla

---

<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 20, 2023, 2:08pm UTC](https://forum.ansible.com/t/template-for-rows-in-a-table/38537/2 "2023-10-20T14:08:28Z")

</div>

Declare the list of columns

&nbsp;&nbsp;fnames: [color, cities, lang]

and get the columns

&nbsp;&nbsp;cols: |  
&nbsp;&nbsp;&nbsp;&nbsp;{% filter from\_yaml %}  
&nbsp;&nbsp;&nbsp;&nbsp;{% for i in fnames %}  
&nbsp;&nbsp;&nbsp;&nbsp;- {{ lookup('vars', i ) }}  
&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;{% endfilter %}

gives

&nbsp;&nbsp;cols:  
&nbsp;&nbsp;&nbsp;&nbsp;- [green, red, yellow, blue]  
&nbsp;&nbsp;&nbsp;&nbsp;- [toronto, montreal, mumbai, dubai]  
&nbsp;&nbsp;&nbsp;&nbsp;- [English, French, Hindi, Arabic]

Find the maximal lengths

&nbsp;&nbsp;max: "{{ cols|map('map', 'length')|map('max') }}"

gives

&nbsp;&nbsp;max: [6, 8, 7]

and create the list of formats

&nbsp;&nbsp;frmt: |  
&nbsp;&nbsp;&nbsp;&nbsp;{% filter from\_yaml %}  
&nbsp;&nbsp;&nbsp;&nbsp;{% for i in max %}  
&nbsp;&nbsp;&nbsp;&nbsp;- '%-{{ i }}s '  
&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;{% endfilter %}

gives

&nbsp;&nbsp;frmt: ['%-6s ', '%-8s ', '%-7s ']

Get the rows

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rows: "{{ rows|d(cols.0)|zip(item)|map('flatten') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ cols[1:] }}"

gives

&nbsp;&nbsp;rows:  
&nbsp;&nbsp;&nbsp;&nbsp;- [green, toronto, English]  
&nbsp;&nbsp;&nbsp;&nbsp;- [red, montreal, French]  
&nbsp;&nbsp;&nbsp;&nbsp;- [yellow, mumbai, Hindi]  
&nbsp;&nbsp;&nbsp;&nbsp;- [blue, dubai, Arabic]

and write the table

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for i in range(fnames|length) %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ frmt[i] % fnames[i] }}{% endfor %}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for j in rows %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for i in range(fnames|length) %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ frmt[i] % j[i] }}{% endfor %}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

gives

&nbsp;&nbsp;msg: |-  
&nbsp;&nbsp;&nbsp;&nbsp;color cities lang  
&nbsp;&nbsp;&nbsp;&nbsp;green toronto English  
&nbsp;&nbsp;&nbsp;&nbsp;red montreal French  
&nbsp;&nbsp;&nbsp;&nbsp;yellow mumbai Hindi  
&nbsp;&nbsp;&nbsp;&nbsp;blue dubai Arabic

---

<div class="post-metadata">

**Author:** ![abhijeetJanwalkar](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@abhijeetJanwalkar](https://forum.ansible.com/u/abhijeetJanwalkar)\
**Post date:** [October 20, 2023, 2:25pm UTC](https://forum.ansible.com/t/template-for-rows-in-a-table/38537/3 "2023-10-20T14:25:03Z")

</div>

Hi Vladimir,

I am really interested in learning how you provide such code.  
From where can I learn more?

Warm regards,  
Abhi

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [October 20, 2023, 3:40pm UTC](https://forum.ansible.com/t/template-for-rows-in-a-table/38537/4 "2023-10-20T15:40:56Z")

</div>

Hi Vladimir,

Thanks . It helps to learn a lot.  
Any recommended link to learn the jinja2 formatting?.

in btw, the “cols” are printed as like below (not as [green, red, yellow, blue]).  
is there ansible/jinja version conflict or I miss a bit?

- name: playbook to print the list as table  
hosts: localhost  
vars:  
color:
- green
- red
- yellow
- blue  
cities:
- toronto
- montreal
- mumbai
- dubai  
lang:
- English
- French
- Hindi
- Arabic  
fnames: [color, cities, lang]  
cols: |  
{% filter from\_yaml %}  
{% for i in fnames %}
- {{ lookup(‘vars’, i ) }}  
{% endfor %}  
{% endfilter %}  
max: “{{ cols|map(‘map’, ‘length’)|map(‘max’) }}”  
frmt: |  
{% filter from\_yaml %}  
{% for i in max %}
- '%-{{ i }}s ’  
{% endfor %}  
{% endfilter %}

tasks:

- name: print the cols  
ansible.builtin.debug:  
msg: “{{cols}}”

- name: print the max  
ansible.builtin.debug:  
msg: “{{max}}”

- name: print the frmt  
ansible.builtin.debug:  
msg: “{{frmt}}”

- set\_fact:  
rows: “{{ rows|d(cols.0)|zip(item)|map(‘flatten’) }}”  
loop: “{{ cols[1:] }}”

- name: print the frmt  
ansible.builtin.debug:  
msg: “{{rows}}”

And the output is as below

PLAY [playbook to print the list as table] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Friday 20 October 2023 18:24:15 +0300 (0:00:00.036) 0:00:00.036 \*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [print the cols] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Friday 20 October 2023 18:24:18 +0300 (0:00:02.702) 0:00:02.738 \*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
[  
“green”,  
“red”,  
“yellow”,  
“blue”  
],  
[  
“toronto”,  
“montreal”,  
“mumbai”,  
“dubai”  
],  
[  
“English”,  
“French”,  
“Hindi”,  
“Arabic”  
]  
]  
}

TASK [print the max] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Friday 20 October 2023 18:24:18 +0300 (0:00:00.125) 0:00:02.863 \*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
6,  
8,  
7  
]  
}

TASK [print the frmt] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Friday 20 October 2023 18:24:18 +0300 (0:00:00.109) 0:00:02.973 \*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
"%-6s ",  
"%-8s ",  
"%-7s "  
]  
}

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Friday 20 October 2023 18:24:18 +0300 (0:00:00.111) 0:00:03.085 \*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=[‘toronto’, ‘montreal’, ‘mumbai’, ‘dubai’])  
ok: [localhost] =\> (item=[‘English’, ‘French’, ‘Hindi’, ‘Arabic’])

TASK [print the frmt] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Friday 20 October 2023 18:24:18 +0300 (0:00:00.075) 0:00:03.161 \*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
[  
“green”,  
“toronto”,  
“English”  
],  
[  
“red”,  
“montreal”,  
“French”  
],  
[  
“yellow”,  
“mumbai”,  
“Hindi”  
],  
[  
“blue”,  
“dubai”,  
“Arabic”  
]  
]  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

---

<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 20, 2023, 5:56pm UTC](https://forum.ansible.com/t/template-for-rows-in-a-table/38537/5 "2023-10-20T17:56:05Z")

</div>

> the "cols" are printed as like below (not as [green, red, yellow,  
> blue]).  
> ...  
> &nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"green",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"toronto",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"English"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"red",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"montreal",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"French"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"yellow",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mumbai",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Hindi"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"blue",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"dubai",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Arabic"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
> &nbsp;&nbsp;&nbsp;&nbsp;]

> \>  
> \>\> msg: |-  
> \>\> color cities lang  
> \>\> green toronto English  
> \>\> red montreal French  
> \>\> yellow mumbai Hindi  
> \>\> blue dubai Arabic

What you see depends on the callback. Try

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

Learn about formatting:

\* Try filters: \*to\_yaml\*, \*to\_nice\_yaml\*. Try the option \*indent\*  
\* Try the filter \*indent\*  
\* Try other callbacks

Links:

[https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_filters.html#formatting-data-yaml-and-json](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#formatting-data-yaml-and-json)  
[https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.indent](https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.indent)  
[https://docs.ansible.com/ansible/latest/plugins/callback.html](https://docs.ansible.com/ansible/latest/plugins/callback.html)  
[https://docs.ansible.com/ansible/latest/collections/index\_callback.html](https://docs.ansible.com/ansible/latest/collections/index_callback.html)  
[https://docs.ansible.com/ansible/latest/reference\_appendices/config.html#default-stdout-callback](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-stdout-callback)

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [October 26, 2023, 4:58am UTC](https://forum.ansible.com/t/template-for-rows-in-a-table/38537/6 "2023-10-26T04:58:14Z")

</div>

Thanks Vladimir, … Very much helpful …
