TransWikia.com

i need to generate ansible-playbook using jinja2 with csv file in ansible

Unix & Linux Asked by Vithushan Rasalingam on December 6, 2021

i need to create jinja templates to generate ansible playbook for that i need to read datas from csv file

csv file is similar to below(file name ansi.csv)

aaa,bbb,ccc,ddd
aa01,ansi,directory,yes
aa02,jinj,directory,yes
aa01,play,direvtory,yes
aa02,tem,directory,yes

and my playbook to generate template is


---
- hosts: localhost
  vars: 
    csvfile: "{{ lookup('file', 'csv_files/ansi.csv')}}"
  tasks:
  - name: generate template
    template:
       src: template.j2
       dest: playbook.yml

and i have created template like bellow

---
{% for item in csvfile.split("n") %}
{% if loop.index != 1 %}
{%   set list = item.split(",") %}
- name: 'make directory'
  hosts: {{ list[0]|trim()}}
  become: {{ list[3]}}
  tasks:
  - name: {{ list[1] }}
    file:
      path: {{list[1]}}
      state: {{ list[2] }}
{%  endif %}
{% endfor %}

out put playbook i’m getting is something simler to bellow

---
- name: 'make directory'
  hosts: aa01
  become: yes
  tasks:
  - name: ansi
    file:
      path: ansi
      state: directory
- name: make directory
  hosts: aa02
  become: yes
  tasks:
  - name: jinj
    file:
      path: jinj
      state: directory
- name: make directory
  hosts: aa01
  become: yes
  tasks:
  - name: play
    file:
      path: play
      state: directory
- name: make directory
  hosts: aa01
  become: yes
  tasks:
  - name: tem
    file:
      path: tem
      state: directory

but need to get playbook like bellow


---
- name: 'make directory'
  hosts: aa01
  become: yes
  tasks:
  - name: ansi
    file:
      path: ansi
      state: directory

  - name: play
    file:
      path: play
      state: directory

- name: make directory
  hosts: aa02
  become: yes
  tasks:
  - name: jinj
    file:
      path: jinj
      state: directory

  - name: tem
    file:
      path: tem
      state: directory

in above playbook my expectation is to group by 1st column and only i have to repeat tasks section(if hosts are same), can some one help me to achieve this?
Thanks in advance

One Answer

Read the csv file with the module read_csv and use the filter groupby. For example the playbook and the template below

shell> cat playbook.yml
- hosts: localhost
  tasks:
    - read_csv:
        path: ansi.csv
      register: data
    - template:
        src: template.j2
        dest: playbook.yml
shell> cat template.j2
---
{% for host in data.list|groupby('aaa') %}
- name: 'make directory'
  hosts: {{ host.0 }}
  become: yes
  tasks:
{% for task in host.1 %}
    - name: {{ task.bbb }}
      file:
        path: {{ task.bbb }}
        state: {{ task.ccc }}

{% endfor %}
{% endfor %}

give

shell> cat playbook.yml 
---
- name: 'make directory'
  hosts: aa01
  become: yes
  tasks:
    - name: ansi
      file:
        path: ansi
        state: directory

    - name: play
      file:
        path: play
        state: direvtory

- name: 'make directory'
  hosts: aa02
  become: yes
  tasks:
    - name: jinj
      file:
        path: jinj
        state: directory

    - name: tem
      file:
        path: ten
        state: directory

Answered by Vladimir Botka on December 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP