I have a playbook where I am using csv file to create a vars file. The for loop iterates through the csvfile, skips the first title line then splits each line into a list. Works fine, but I was hoping to instead of skipping the title line, to pull out the names and use as the variable name instead of hard coding the names like Type, Hostname, etc. This would allow me to input csv files that have different title names and more or less names.
csv file example:
Type,Hostname,IP_Address,Gateway,DNS_Server,NTP_Server
ubuntu,lab1,10.1.1.1,10.1.1.254,192.168.1.254.192.168.1.253
centos,lab2,10.1.1.2,10.1.1.254,192.168.1.254.192.168.1.253
rhel6,lab3,10.1.1.3,10.1.1.254,192.168.1.254.192.168.1.253
rhel7,lab4,10.1.1.4,10.1.1.254,192.168.1.254.192.168.1.253
loop code:
---
servers:
{% for item in csvfile.split("\n") %}
{% if loop.index != 1 %}
{% set list = item.split(",") %}
{{ list[1]|trim() }}:
Type: {{ list[0]|trim() }}
Hostname: {{ list[1]|trim() }}
IP_Address: {{ list[2]|trim() }}
Gateway: {{ list[3]|trim() }}
DNS_Server: {{ list[4]|trim() }}
NTP_Server: {{ list[22]|trim() }}
{% endif %}
{% endfor %}
Question I have is, how would I pull out loop.index. 1 and use the title names as variables? Something like this:
{{ title. }}: {{ list[0]|trim() }}
{{ title. }}: {{ list[1]|trim() }}
{{ title }}: {{ list[2]|trim() }}
{{ title }}: {{ list[3]|trim() }}
{{ title }}: {{ list[4]|trim() }}
{{ title }}: {{ list[22]|trim() }}