- hosts: localhost gather_facts: false become: false vars: csv_hosts: "{{ out.stdout_lines|map('split', ';')|groupby('0') }}" ini_hosts: | {% for group in csv_hosts %} [{{ group.0 }}] {% for host in group.1|map(attribute='1') %} {{ host }} {% endfor %} {% endfor %} tasks: - shell: scp linuxadmin@server:/etc/linuxmuster/sophomorix/default-school/devices.csv . - shell: cat devices.csv | grep -v ^# | grep -v ^$ register: out - copy: dest: devices.ini content: "{{ ini_hosts }}"