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

**URL:** <https://forum.ansible.com/t/can-someone-show-a-working-example-of-how-to-iterate-over-the-rows-of-a-csv-file/26815>\
**Category:** Ansible Project\
**Created:** [February 21, 2018, 3:35pm UTC](https://forum.ansible.com/t/can-someone-show-a-working-example-of-how-to-iterate-over-the-rows-of-a-csv-file/26815 "2018-02-21T15:35:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nico\_Sabbi](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nico\_Sabbi](https://forum.ansible.com/u/Nico_Sabbi)\
**Post date:** [February 21, 2018, 3:35pm UTC](https://forum.ansible.com/t/can-someone-show-a-working-example-of-how-to-iterate-over-the-rows-of-a-csv-file/26815/1 "2018-02-21T15:35:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 21, 2018, 6:58pm UTC](https://forum.ansible.com/t/can-someone-show-a-working-example-of-how-to-iterate-over-the-rows-of-a-csv-file/26815/2 "2018-02-21T18:58:03Z")

</div>

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  
&nbsp;&nbsp;debug: var=item  
&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Nico\_Sabbi](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nico\_Sabbi](https://forum.ansible.com/u/Nico_Sabbi)\
**Post date:** [February 22, 2018, 1:25pm UTC](https://forum.ansible.com/t/can-someone-show-a-working-example-of-how-to-iterate-over-the-rows-of-a-csv-file/26815/3 "2018-02-22T13:25:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Claudia\_de\_Luna](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudia\_de\_Luna](https://forum.ansible.com/u/Claudia_de_Luna)\
**Post date:** [February 24, 2018, 3:13pm UTC](https://forum.ansible.com/t/can-someone-show-a-working-example-of-how-to-iterate-over-the-rows-of-a-csv-file/26815/4 "2018-02-24T15:13:12Z")

</div>

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

`

---

<div class="post-metadata">

**Author:** ![bill\_paxton](https://avatars.discourse-cdn.com/v4/letter/b/46a35a/32.png) [@bill\_paxton](https://forum.ansible.com/u/bill_paxton)\
**Post date:** [February 27, 2018, 6:38pm UTC](https://forum.ansible.com/t/can-someone-show-a-working-example-of-how-to-iterate-over-the-rows-of-a-csv-file/26815/5 "2018-02-27T18:38:49Z")

</div>

Take a look at [csv\_to\_facts module](https://github.com/joelwking/ansible-nxapi)

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.

---

<div class="post-metadata">

**Author:** ![bill\_paxton](https://avatars.discourse-cdn.com/v4/letter/b/46a35a/32.png) [@bill\_paxton](https://forum.ansible.com/u/bill_paxton)\
**Post date:** [February 27, 2018, 6:58pm UTC](https://forum.ansible.com/t/can-someone-show-a-working-example-of-how-to-iterate-over-the-rows-of-a-csv-file/26815/6 "2018-02-27T18:58:20Z")

</div>

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.

`
