# CSV File sample.csvhost, user, password, comment,1.1.1.1, claudia, mypassword, the first one1.1.1.2, elvira, epassword, the second one1.1.1.3, billy, bpassword, the third one
---- 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 }}"
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#
---
- 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