# Want to access the server information from CSV file.

**URL:** <https://forum.ansible.com/t/want-to-access-the-server-information-from-csv-file/28763>\
**Category:** Ansible Project\
**Created:** [October 11, 2018, 3:46am UTC](https://forum.ansible.com/t/want-to-access-the-server-information-from-csv-file/28763 "2018-10-11T03:46:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shyam](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@Shyam](https://forum.ansible.com/u/Shyam)\
**Post date:** [October 11, 2018, 3:46am UTC](https://forum.ansible.com/t/want-to-access-the-server-information-from-csv-file/28763/1 "2018-10-11T03:46:11Z")

</div>

Hi all,

Im trying to access the server information from CSV file. Based on this information, I want to target the host.

The CSV file contains the following information. based on this CSV file, when ever we pass the instance name using --extra-vars the task should execute on the corresponding server.

Is it possible. Can any one help me with the playbook.

instance name servername

instance1 server1  
instance2 server2  
instance3 server3  
instance4 server1  
instance5 server2  
instance6 server3  
… '…  
… …  
… …  
… …  
instance100 server3

---

<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:** [October 11, 2018, 3:25pm UTC](https://forum.ansible.com/t/want-to-access-the-server-information-from-csv-file/28763/2 "2018-10-11T15:25:31Z")

</div>

[https://github.com/joelwking/ansible-nxapi](https://github.com/joelwking/ansible-nxapi)

I’ve used the csv\_to\_facts for pulling in facts for network devices. works really well. Just place csv\_to\_facts.py in the library/ folder then:

- name: import csv {{ glob\_item }}  
csv\_to\_facts:  
src: “{{ glob\_item }}”  
table: switchPorts

look at documentation inside the .py

---

<div class="post-metadata">

**Author:** ![Shyam](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@Shyam](https://forum.ansible.com/u/Shyam)\
**Post date:** [October 11, 2018, 5:32pm UTC](https://forum.ansible.com/t/want-to-access-the-server-information-from-csv-file/28763/3 "2018-10-11T17:32:50Z")

</div>

Hi Bill,

Thanks for your information. I will check it.

I have tried the following,

```auto

- name: msg="Match 'Li' on the first column, but return the 3rd column (columns start counting after the match)"
  debug: msg="The atomic mass of Lithium is {{ lookup('csvfile', 'Li file=elements.csv delimiter=, col=2') }}"

```

```auto
and it is returning the hostname. I want to save the result hostname in a variable and will used to execute the tasks.

```

```auto
Can you suggest ?

```

```auto

```

---

<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:** [October 11, 2018, 6:37pm UTC](https://forum.ansible.com/t/want-to-access-the-server-information-from-csv-file/28763/4 "2018-10-11T18:37:03Z")

</div>

create these files, see if this gives you the idea…

serverlist.yml → [https://pastebin.com/eFdCbP6yserverlist.csv](https://pastebin.com/eFdCbP6yserverlist.csv) → [https://pastebin.com/4JTtr2vv](https://pastebin.com/4JTtr2vv)

then play with using “when” to control task execution, like:

- name: dump my\_server  
debug: var=my\_server  
when: my\_server == ‘server1’

---

<div class="post-metadata">

**Author:** ![Shyam](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@Shyam](https://forum.ansible.com/u/Shyam)\
**Post date:** [October 11, 2018, 7:25pm UTC](https://forum.ansible.com/t/want-to-access-the-server-information-from-csv-file/28763/5 "2018-10-11T19:25:45Z")

</div>

Hi Bill,

Thank you so much for your help. I looking for this.

Thank you.
