Can someone show a working example of how to iterate over the rows of a csv file?

4,767 views
Skip to first unread message

Nico Sabbi

unread,
Feb 21, 2018, 10:35:23 AM2/21/18
to Ansible Project
HI,
I find Ansible  docs *really* frustrating. Never a full example  that is not trivial or misleading.

Please, can someone provide a working example of a play to iterate over the rows of a csvfile?

Thanks.

Brian Coca

unread,
Feb 21, 2018, 1:58:28 PM2/21/18
to Ansible Project
there are plenty of specific examples, just your specific case is not
covered, but it is simple enough to inffer from existing ones,
something like this should work:

- name: show the row
debug: var=item
loop: " {{ lookup('file', '/path/tocsv') }}"


you might need to split on ',' if you want the columns, there is a
csvfile lookup it it retrieves specific column, not rows


--
----------
Brian Coca

Nico Sabbi

unread,
Feb 22, 2018, 8:25:34 AM2/22/18
to Ansible Project
Sorry, but  it's not clear at all how debug iterates over the rows. 
Running a simple debug doesn't help.

For example, if I had a playbook like this (that of course doesn't work), how should I fix it
to iterate over every single row and print the value of the 3 columns?

---
  - hosts: db
    become: true
    remote_user: root
    vars:
      csvfile: "{{ lookup('csvfile', 'file=file.csv delimiter=,') }}"
      
    tasks:
      - name: cat
        debug: msg="{{ item.a }} - {{ item.a }} - {{ item.c }}"  #for every row print the value of a, b and c columns
        with_items: csvfile
  

Thanks.

Claudia de Luna

unread,
Feb 24, 2018, 10:13:13 AM2/24/18
to Ansible Project
Hi Nico,

Not sure if this is what you are looking for but for a sample CSV file


# CSV File sample.csv
host, user, password, comment,
1.1.1.1, claudia, mypassword, the first one
1.1.1.2, elvira, epassword, the second one
1.1.1.3, billy, bpassword, the third one

with this playbook sample_csv.yml
---
- hosts: localhost
  connection: local
  gather_facts: False
  ignore_errors: yes

  vars:
    contents: "{{ lookup('file', './sample.csv') }}"
    lines: "{{ contents.split('\n') }}"

  tasks:
    - debug: msg="Iterating over CSV lines = {{ item }}"
      with_items: 
        - "{{ lines }}"


you can get this output:

root@127c868b9dd3:/ansible/ansible2_4_base# ansible-playbook -i hosts sample_csv.yml

PLAY [localhost] ****************************************************************************************************************************

TASK [debug] ********************************************************************************************************************************
ok: [localhost] => (item=# CSV File sample.csv) => {
    "item": "# CSV File sample.csv",
    "msg": "Iterating over CSV lines = # CSV File sample.csv"
}
ok: [localhost] => (item=host, user, password, comment,) => {
    "item": "host, user, password, comment,",
    "msg": "Iterating over CSV lines = host, user, password, comment,"
}
ok: [localhost] => (item=1.1.1.1, claudia, mypassword, the first one) => {
    "item": "1.1.1.1, claudia, mypassword, the first one",
    "msg": "Iterating over CSV lines = 1.1.1.1, claudia, mypassword, the first one"
}
ok: [localhost] => (item=1.1.1.2, elvira, epassword, the second one) => {
    "item": "1.1.1.2, elvira, epassword, the second one",
    "msg": "Iterating over CSV lines = 1.1.1.2, elvira, epassword, the second one"
}
ok: [localhost] => (item=1.1.1.3, billy, bpassword, the third one# CSV File) => {
    "item": "1.1.1.3, billy, bpassword, the third one# CSV File",
    "msg": "Iterating over CSV lines = 1.1.1.3, billy, bpassword, the third one# CSV File"
}

PLAY RECAP **********************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

root@127c868b9dd3:/ansible/ansible2_4_base#

you can further split each line by "," but I'm wondering if what you are really looking for is a data structure you can put in group_vars or host_vars.

This ultimately becomes string parsing and as a general rule you want to stay away from that as much as you possibly can.

If you turn your CSV iinto a YAML file you have a bit more control your items and you can refer to them explicitly.

Food for thought but it all depends on what you are trying to do.

Hope this helps!

Claudia

bill paxton

unread,
Feb 27, 2018, 1:38:50 PM2/27/18
to Ansible Project
Take a look at csv_to_facts module

I found this to be quick and dirty way to access data from a spreadsheet -> converted to csv first.  Look inside the .py for extra info on how to use it.


bill paxton

unread,
Feb 27, 2018, 1:58:20 PM2/27/18
to Ansible Project
Oh, and you can drop the csv_to_facgts.py in ./library in your project folder...your playbook should then be able to use the module.

---


 
- name: import csv
    csv_to_facts
:
      src
: "{{ some_csv_file }}"  
      table
: mainTable              # optionally name the table....helpful if your playbook later imports add'l csv's


   
- name: "loop through 'Spine' rows and create directories and switch configs"
    include
: core_loop_rows.yml
      destination
={{ base_dir }}/{{ item1['zone'] }}
      zone
={{ item1['zone'] }}
      building
={{ item1['building'] }}
      location
={{ item1['locationName']}}
      host_name
={{ item1['hostName'] }}
      device_type
={{ item1['deviceType'] }}
      core_link_1
={{ item1['int1'] }}
      core_link_2
={{ item1['int2'] }}
      farend_core_link_1
={{ item1['int1'] }}
      farend_core_link_2
={{ item1['int2'] }}
      default_iface_mask
={{ item1['mask'] }}
      core_link_1_ip
={{ item1['int1_ip'] }}
      core_link_2_ip
={{ item1['int2_ip'] }}
      core_1_name
='core1'
      core_2_name
='core2'
      loop0_ip
={{ item1['Loop0'] }}
    with_items
: "{{ mainTable }}"     # Now we can loop through the rows of the csv and access cells by the column header name
                                     
# e.g. my csv file has column headers "Loop0", "locationName", "zone", etc.
    loop_control
:
      loop_var
: item1
Reply all
Reply to author
Forward
0 new messages