Using variables in ansible script after fetching from csv files inside a loop
Use read_csv – Read a CSV file module. For example the playbook
- hosts: localhost
tasks:
- read_csv:
path: vms.csv
key: hostname
register: vms
- debug:
msg: "{{ item.key }}:
{{ item.value.hostname }},
{{ item.value.partition }},
{{ item.value.ip }}"
loop: "{{ vms.dict|dict2items }}"
with the file
$ cat vms.csv
hostname,partition,ip
hostname1,/dev/sda1,10.1.0.21
hostname2,/dev/sda1,10.1.0.22
hostname3,/dev/sda1,10.1.0.23
gives
msg: 'hostname1: hostname1, /dev/sda1, 10.1.0.21'
msg: 'hostname2: hostname2, /dev/sda1, 10.1.0.22'
msg: 'hostname3: hostname3, /dev/sda1, 10.1.0.23'