For example, given the files
shell> tree fruits/
fruits/
├── apples.csv
├── grapes.csv
└── pears.csv
0 directories, 3 files
shell> cat fruits/apples.csv
red,big,20
green,small,10
shell> cat fruits/grapes.csv
red,big,20
black,small,10
shell> cat fruits/pears.csv
green,big,30
yellow,small,20
Read the files
- read_csv:
fieldnames: color,size,price
path: "{{ item }}"
with_fileglob: fruits/*.csv
register: fruit
Instead of creating the variables fruit_*, creating a dictionary of the fruits is simpler. For example, put the declarations below as appropriate
fruits: "{{ dict(f_keys | zip(f_vals)) }}"
f_vals: "{{ fruit.results | map(attribute='list') | list }}"
f_keys: "{{ fruit.results | map(attribute='item')
| map('basename')
| map('splitext')
| map('first') | list }}"
gives
fruits:
apples:
- {color: red, price: '20', size: big}
- {color: green, price: '10', size: small}
grapes:
- {color: red, price: '20', size: big}
- {color: black, price: '10', size: small}
pears:
- {color: green, price: '30', size: big}
- {color: yellow, price: '20', size: small}
Example of a complete playbook
- hosts: localhost
vars:
fruits: "{{ dict(f_keys | zip(f_vals)) }}"
f_vals: "{{ fruit.results | map(attribute='list') | list }}"
f_keys: "{{ fruit.results | map(attribute='item')
| map('basename')
| map('splitext')
| map('first') | list }}"
tasks:
- read_csv:
fieldnames: color,size,price
path: "{{ item }}"
with_fileglob: fruits/*.csv
register: fruit
- debug:
var: fruits
- debug:
var: fruits.apples