Want to access the server information from CSV file.

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

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

Hi Bill,

Thanks for your information. I will check it.

I have tried the following,


- 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') }}"
and it is returning the hostname. I want to save the result hostname in a variable and will used to execute the tasks.
Can you suggest ?

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

serverlist.yml → https://pastebin.com/eFdCbP6yserverlist.csvhttps://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’

Hi Bill,

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

Thank you.