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




On Friday, October 20, 2023 at 7:55:34 PM UTC+5:30 Abhijeet Janwalkar wrote:

> Hi Vladimir,
>
> I am really interested in learning how you provide such code.
> From where can I learn more?
>
> Warm regards,
> Abhi
>
> On Fri, 20 Oct 2023, 16:08 Vladimir Botka <[email protected]> wrote:
>
>> Declare the list of columns
>>
>>   fnames: [color, cities, lang]
>>
>> and get the columns
>>
>>   cols: |
>>     {% filter from_yaml %}
>>     {% for i in fnames %}
>>     - {{ lookup('vars', i ) }}
>>     {% endfor %}
>>     {% endfilter %}
>>
>> gives
>>
>>   cols:
>>     - [green, red, yellow, blue]
>>     - [toronto, montreal, mumbai, dubai]
>>     - [English, French, Hindi, Arabic]
>>
>> Find the maximal lengths
>>
>>   max: "{{ cols|map('map', 'length')|map('max') }}"
>>
>> gives
>>
>>   max: [6, 8, 7]
>>
>> and create the list of formats
>>
>>   frmt: |
>>     {% filter from_yaml %}
>>     {% for i in max %}
>>     - '%-{{ i }}s   '
>>     {% endfor %}
>>     {% endfilter %}
>>
>> gives
>>
>>   frmt: ['%-6s   ', '%-8s   ', '%-7s   ']
>>
>> Get the rows
>>
>>     - set_fact:
>>         rows: "{{ rows|d(cols.0)|zip(item)|map('flatten') }}"
>>       loop: "{{ cols[1:] }}"
>>
>> gives
>>
>>   rows:
>>     - [green, toronto, English]
>>     - [red, montreal, French]
>>     - [yellow, mumbai, Hindi]
>>     - [blue, dubai, Arabic]
>>
>>  and write the table
>>
>>     - debug:
>>         msg: |
>>           {% for i in range(fnames|length) %}
>>           {{ frmt[i] % fnames[i] }}{% endfor %}
>>
>>           {% for j in rows %}
>>           {% for i in range(fnames|length) %}
>>           {{ frmt[i] % j[i] }}{% endfor %}
>>
>>           {% endfor %}
>>
>> gives
>>
>>   msg: |-
>>     color    cities     lang
>>     green    toronto    English
>>     red      montreal   French
>>     yellow   mumbai     Hindi
>>     blue     dubai      Arabic
>>
>> -- 
>> Vladimir Botka
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/20231020160828.02da4038%40gmail.com
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/b9c39508-d1b5-4daa-be5f-b0265a3b96aan%40googlegroups.com.

Reply via email to