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
#
#
#ansible --version
ansible [core 2.13.11]
config file = /home/user/ansible/test-snippets/ansible.cfg
configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/user/ansible/kube-ansible/lib/python3.8/site-packages/ansible
ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
executable location = /home/user/ansible/kube-ansible/bin/ansible
python version = 3.8.10 (default, May 26 2023, 14:05:08) [GCC 9.4.0]
jinja version = 3.1.2
libyaml = True