Home > database >  Ansible (read_csv): How to convert entire CSV column into a list efficiently without loop?
Ansible (read_csv): How to convert entire CSV column into a list efficiently without loop?

Time:04-25

I've a CSV (circa 2200 rows) with around 8 columns. For simplicity below is a snapshot

ID,fruit,currency
11,mango,USD
12,peach,GBP
13,apricot,EUR

The aim is to get the fruit column as an list and write to another file

["mango","peach","apricot"]

I've done it using loop, but this take ages to loop through larger list (below is the concept i've done)

    - name: "Read as a list"
      read_csv:
        path: ./fruit.csv
      register: mycsv

    - debug:
        msg: '{{ item.fruit| join (',') }}'
      loop: "{{mycsv.list}}"

Is there a efficient/better way to convert/translate above read_csv specific column into a list?

CodePudding user response:

For example

fruit: "{{ mycsv.list|map(attribute='fruit')|list }}"

gives the list

fruit: [mango, peach, apricot]
  • Related