Hi  Vladimir,

Thanks . It helps to learn a lot.
Any useful like to learn jijna2 in details will be helpful .

in Btw,  why the "cols" are printed as like below , instead of you have 
mentioned?  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
      debug: 
        msg: "{{cols}}"

    - name: print the max
      debug: 
        msg: "{{max}}"

    - name: print the frmt
      debug: 
        msg: "{{frmt}}"
    
    - set_fact:
        rows: "{{ rows|d(cols.0)|zip(item)|map('flatten') }}"
      loop: "{{ cols[1:] }}"

    - name: print the frmt
      debug: 
        msg: "{{rows}}"






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/91dfa286-4ba4-4792-a5d5-8866b755bce8n%40googlegroups.com.

Reply via email to