# Loop through CSV file

**URL:** <https://forum.ansible.com/t/loop-through-csv-file/36484>\
**Category:** Ansible Project\
**Created:** [March 27, 2022, 7:58pm UTC](https://forum.ansible.com/t/loop-through-csv-file/36484 "2022-03-27T19:58:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Delmiro\_Campelo](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Delmiro\_Campelo](https://forum.ansible.com/u/Delmiro_Campelo)\
**Post date:** [March 27, 2022, 7:58pm UTC](https://forum.ansible.com/t/loop-through-csv-file/36484/1 "2022-03-27T19:58:07Z")

</div>

I was hoping that you can give me a hand with an issue here. I have about a thousand routers that I’m trying to configure. I have all the device specific information on a CSV file that I would like to use for populating all the necessary device fields. I would like to loop through this CSV file, so I don’t have create a playbook with a thousand tasks. I have a playbook that works on a single device basis, but I want to expand it to be able to use for as many routers as I can have on a CSV file. Can you give me some ideas here? Thank you for any help or tips you can provide.

- hosts: vmanage

connection: local  
gather\_facts: no  
vars:  
ansible\_user: testing123  
vmanage\_ip: 10.10.2.2  
ansible\_password: testing123  
state: present  
host\_name\_var: “{{ lookup(‘csvfile’,‘123456 file=csvdata.csv delimiter=, col=1’) }}”  
system\_ip\_var: “{{ lookup(‘csvfile’,‘123456 file=csvdata.csv delimiter=, col=2’) }}”  
site\_id\_var: “{{ lookup(‘csvfile’,‘123456 file=csvdata.csv delimiter=, col=3’) }}”  
vpn\_next\_hop\_0\_var: “{{ lookup(‘csvfile’,‘123456 file=csvdata.csv delimiter=, col=4’) }}”  
vpn\_next\_hop\_1\_var: “{{ lookup(‘csvfile’,‘123456 file=csvdata.csv delimiter=, col=5’) }}”  
vpn\_if\_ipv4\_1\_var: “{{ lookup(‘csvfile’,‘123456 file=csvdata.csv delimiter=, col=6’) }}”  
vpn\_if\_ipv4\_2\_var: “{{ lookup(‘csvfile’,‘123456 file=csvdata.csv delimiter=, col=7’) }}”  
vpn\_if\_ipv4\_var: “{{ lookup(‘csvfile’,‘123456 file=csvdata.csv delimiter=, col=8’) }}”

tasks:

- vmanage\_device\_attachment:  
user: “{{ ansible\_user }}”  
host: “{{ vmanage\_ip }}”  
password: “{{ ansible\_password }}”  
validate\_certs: false  
device: Branch2  
template: BRANCHES-CSR-TEMPLATEv2  
variables:  
vpn\_next\_hop\_ip\_address\_0: “{{ vpn\_next\_hop\_0\_var }}”  
vpn\_next\_hop\_ip\_address\_1: “{{ vpn\_next\_hop\_1\_var }}”  
vpn\_if\_ipv4\_address\_2: “{{ vpn\_if\_ipv4\_1\_var }}”  
vpn\_if\_ipv4\_address: “{{ vpn\_if\_ipv4\_2\_var }}”  
host-name: “{{ host\_name\_var }}”  
system-ip: “{{ system\_ip\_var }}”  
site-id: “{{ site\_id\_var }}”  
vpn\_if\_ipv4\_address\_1: “{{ vpn\_if\_ipv4\_var }}”  
wait: yes  
state: “{{ state }}”

cat /etc/ansible/python-viptela/ansible/vars/csvdata.csv  
system-uuid,hostname,system-ip,site-id,vpn\_next\_hop\_ip\_address\_0,vpn\_next\_hop\_ip\_address\_1,vpn\_if\_ipv4\_address\_2,vpn\_if\_ipv4\_address,vpn\_if\_ipv4\_address\_1  
123456,Branch2,100.1.4.1,141,98.1.9.2, 98.1.10.2,98.1.9.1/30,98.1.10.1/30,10.141.0.254/24

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 27, 2022, 9:06pm UTC](https://forum.ansible.com/t/loop-through-csv-file/36484/2 "2022-03-27T21:06:46Z")

</div>

You’d do well to start here: [https://docs.ansible.com/ansible/latest/collections/community/general/read\_csv\_module.html](https://docs.ansible.com/ansible/latest/collections/community/general/read_csv_module.html)

---

<div class="post-metadata">

**Author:** ![Delmiro\_Campelo](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Delmiro\_Campelo](https://forum.ansible.com/u/Delmiro_Campelo)\
**Post date:** [March 28, 2022, 2:42pm UTC](https://forum.ansible.com/t/loop-through-csv-file/36484/3 "2022-03-28T14:42:54Z")

</div>

Thank you for that suggestion. read\_csv it’s a better option for doing this.
