# Need to pass variable and hostname to a playbook from CSV file.

**URL:** <https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495>\
**Category:** Ansible Project\
**Created:** [March 18, 2020, 2:28pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495 "2020-03-18T14:28:27Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [March 18, 2020, 2:28pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/1 "2020-03-18T14:28:27Z")

</div>

I have a CSV file that contains the IP’s and it’s variables.  
I need to use the details to log in to the host and use the respective variable’s value of the specfic IP’s.

Please help me out with this.

# cat AnsibleTest.csv

ip,var1,var2

10.1.1.1,rchantif1,mechlab  
10.1.1.2,rchans01,contlab

---

<div class="post-metadata">

**Author:** ![J111](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@J111](https://forum.ansible.com/u/J111)\
**Post date:** [March 20, 2020, 7:05pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/2 "2020-03-20T19:05:05Z")

</div>

My csv file looks like this and its called ip.csv:

ip,username,host

10.1.1.1,rchantif1,mechlab

10.1.1.2,rchans01,contlab

192.168.2.142,vagrant

I’m using the read\_csv: module to access the data

What I did is create a playbook that prints what you are looking for the then I followed up on another task an example of logging into a host with the username and IP. I’m not 100% sure what you want to do but hopefully with the info I provided you have a start to your playbook/tasks.

```auto
---

```

```auto
- name: read items from csv and store as vars

```

```auto
  hosts: localhost

```

```auto
  become: false

```

```auto
  tasks:

```

```auto
    - name: read_csv

```

```auto
      read_csv:

```

```auto
        path: ip.csv

```

```auto
      register: ip

```

```auto
    - name: print csv items

```

```auto
      debug:

```

```auto
        msg: "{{ip.list.2.ip}} {{ip.list.2.username}} {{ip.list.2.host}}"

```

```auto
    - name:

```

```auto
      shell: "ssh {{ip.list.2.username}}@{{ip.list.2.ip}} 'ls -la'"

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 20, 2020, 10:27pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/3 "2020-03-20T22:27:53Z")

</div>

> I have a CSV file that contains the IP's and it's variables.  
> I need to use the details to log in to the host and use the respective  
> variable's value of the specfic IP's.
> 
> # cat AnsibleTest.csv  
> ip,var1,var2  
> 10.1.1.1,rchantif1,mechlab  
> 10.1.1.2,rchans01,contlab

Read the the file and create new group of hosts in the first play. The use  
the group in the second play. For example

> cat playbook.yml

- hosts: localhost  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- read\_csv:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: AnsibleTest.csv  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: myhosts  
&nbsp;&nbsp;&nbsp;&nbsp;- add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ item.ip }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: test\_01  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var1: "{{ item.var1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var2: "{{ item.var2 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ myhosts.list }}"

- hosts: test\_01  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ inventory\_hostname }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ var1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ var2 }}"

> ansible-playbook playbook.yml

...  
ok: [10.1.1.1] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"10.1.1.1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"rchantif1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mechlab"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}  
ok: [10.1.1.2] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"10.1.1.2",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"rchans01",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"contlab"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 8, 2020, 2:25pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/4 "2020-04-08T14:25:04Z")

</div>

thanks Vladimir, your suggestion helped a lot. Still, i am facing one challenge while using add\_host. I have the csv file with the same server has different volumes. When i run the playbook, it is displaying last entry associated to the same but i would like to display all the entries. Can you suggest on the same?

# cat mounts.csv

host,remote\_path,mnt\_path  
host1,nfsvol1,mount1  
host1,nfsvol2,mount2  
host1,nfsvol3,mount3  
host2,nfsvol1,mount1  
host2,nfsvol2,mount2  
host2,nfsvol3,mount3

# cat mounts.yml

---

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [April 8, 2020, 3:18pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/5 "2020-04-08T15:18:04Z")

</div>

format your csv data file to inventory file as below. Then the variables can be used to the respective hosts directly.  
Hope this helps.

Use sed and awk for formatting the inventory file from CSV file

sed ‘s/,/ /g’ mounts  
awk -F’ ’ -vOFS=’ ’ ‘{ $2 = “remote\_path=” $2 }2’ mounts  
awk -F’ ’ -vOFS=’ ’ ‘{ $3 = “mnt\_path=” $3 }3’ /tmp/mounts

# cat mounts

host1 remote\_path=nfsvol1 mnt\_path=mount1  
host1 remote\_path=nfsvol2 mnt\_path=mount2  
host1 remote\_path=nfsvol3 mnt\_path=mount3  
host2 remote\_path=nfsvol1 mnt\_path=mount1  
host2 remote\_path=nfsvol2 mnt\_path=mount2  
host2 remote\_path=nfsvol3 mnt\_path=mount3

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 8, 2020, 3:19pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/6 "2020-04-08T15:19:09Z")

</div>

Hi Pandu,

the playbook is working if the csv has single entry associated to the host, but same playbook is failing if the same host has multiple variables entries. I tried many ways by using inventory but no luck. Finally, i choose read\_csv and add\_host. If you have working example with inventory, can you please share with me?

# cat mounts.csv [working as expected]

host,remote\_path,mnt\_path  
host1,nfsvol1,mount1  
host2,nfsvol2,mount2  
host3,nfsvol3,mount3

# cat mounts.csv [working, but it is considering last entry only]

host,remote\_path,mnt\_path  
host1,nfsvol1,mount1  
host1,nfsvol2,mount2  
host1,nfsvol3,mount3  
host2,nfsvol1,mount1  
host2,nfsvol2,mount2  
host2,nfsvol3,mount3

Regards,  
Suresh

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 8, 2020, 3:31pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/7 "2020-04-08T15:31:29Z")

</div>

Initially, i tried your approach but engineer has to do more work to make the format as we are planning to use playbook for nfs volume migration. we have 1000’s of servers with 100’s volumes. Hence, the reason, we chosen the csv format. I have been using inventory based host connection but it is connecting even if volume is not required to mount/unmount on target host.

# cat nfsvolmigration/roles/volmount/tasks/volmount.yml

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 8, 2020, 4:23pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/8 "2020-04-08T16:23:43Z")

</div>

> ... i would like to display all the entries.
> 
> # cat mounts.csv  
> host,remote\_path,mnt\_path  
> host1,nfsvol1,mount1  
> host1,nfsvol2,mount2  
> host1,nfsvol3,mount3  
> host2,nfsvol1,mount1  
> host2,nfsvol2,mount2  
> host2,nfsvol3,mount3
> 
> # cat mounts.yml  
> ---  
> - name: mount the nfsshare in client side  
> &nbsp;&nbsp;hosts: localhost  
> &nbsp;&nbsp;gather\_facts: false  
> &nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: reading volume info from csv  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;read\_csv:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ playbook\_dir }}/mounts.csv"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: sources  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Grouping host and volume information  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add\_host:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.host }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: nfsgroup  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var1: "{{ item.remote\_path }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var2: "{{ item.mnt\_path }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ sources.list }}"

Use "groupby" filter. For example  
[https://jinja.palletsprojects.com/en/master/templates/#groupby](https://jinja.palletsprojects.com/en/master/templates/#groupby)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.0 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: nfsgroup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var1: "{{ item.1|json\_query('.remote\_path') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var2: "{{ item.1|json\_query('.mnt\_path') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ sources.list|groupby('host') }}"

> - name: list volume details  
> &nbsp;&nbsp;hosts: nfsgroup  
> &nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;gather\_facts: false  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ inventory\_hostname }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ var1 }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ var2 }}"

You should receive lists of "remote\_path" and "mnt\_path" in var1 and var2  
respectively.

ok: [host1] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"host1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nfsvol1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nfsvol2",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nfsvol3"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mount1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mount2",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mount3"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}  
ok: [host2] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"host2",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nfsvol1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nfsvol2",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nfsvol3"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mount1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mount2",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mount3"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 8, 2020, 5:45pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/9 "2020-04-08T17:45:31Z")

</div>

awesome, it really helped for my requirement. thank you so much Vladimir,

Regards,  
Suresh

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 9, 2020, 12:16pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/10 "2020-04-09T12:16:42Z")

</div>

Hi Vladimir,

I have one more last question on the same request as we have different python interpreter on some hosts. Is it possible to read the python interpreter variable from CSV file while connecting the particular?  
Prior to your solution, i use to use inventory to pass the variable to the host as below. Now, i want to omit inventory file as i would like to use your solution completely for host inventory. Can you please suggest same to overcome this problem other than declaring variables host\_vars?

host4 ansible\_python\_interpreter=/usr/bin/python2.6

Regards,  
Suresh

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 9, 2020, 3:21pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/11 "2020-04-09T15:21:04Z")

</div>

Hi Suresh,

> Hi Vladimir,
> 
> I have one more last question on the same request as we have different  
> python interpreter on some hosts. Is it possible to read the python  
> interpreter variable from CSV file while connecting the particular?  
> Prior to your solution, i use to use inventory to pass the variable to the  
> host as below. Now, i want to omit inventory file as i would like to use  
> your solution completely for host inventory. Can you please suggest same to  
> overcome this problem other than declaring variables host\_vars?
> 
> host4 ansible\_python\_interpreter=/usr/bin/python2.6
> 
> Regards,  
> Suresh

Yes. It's possible. Simply put the path to Python into the CSV file. For  
example, put it into the first record of the particular host. Various paths  
don't make sense. Right?

> cat mounts.csv

host,remote\_path,mnt\_path,python  
host1,nfsvol1,mount1,/usr/bin/python2.6  
host1,nfsvol2,mount2  
host1,nfsvol3,mount3  
host2,nfsvol1,mount1,/usr/bin/python3.7  
host2,nfsvol2,mount2  
host2,nfsvol3,mount3

Then simply assign "ansible\_python\_interpreter in the first play

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.0 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: nfsgroup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var1: "{{ item.1|json\_query('.remote\_path') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var2: "{{ item.1|json\_query('.mnt\_path') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_python\_interpreter: "{{ item.1.0.python }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ sources.list|groupby('host') }}"

(It's not necessary to delegate\_to localhost when the play is running at  
localhost "hosts: localhost").

HTH,  
&nbsp;&nbsp;vlado

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 14, 2020, 2:02pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/12 "2020-04-14T14:02:58Z")

</div>

Hi Vladimir, thanks for your help all the time.

I tried using the logic, and it worked when apply the values to debug but the same data is passing to mount module with additional quotes and brackets which is causing the errors. Any suggestions please?

Playbook and CSV details:

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 15, 2020, 5:33am UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/13 "2020-04-15T05:33:04Z")

</div>

Would anyone please help me with this?

Regards,  
Suresh

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 27, 2020, 7:46am UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/14 "2020-04-27T07:46:20Z")

</div>

> ... to achieve my goal as iteration[\*have multiple nfs volume in  
> CSV for each host\*] is failing with add\_host module due to \*BYPASS\_HOST\_LOOP  
> = True\* in module destination. ...  
> [...]  
> - name: list the volumes  
> &nbsp;&nbsp;hosts: nfsgroup  
> &nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;gather\_facts: false  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: debug output  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ inventory\_hostname }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ var1 }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ var2 }}"  
> [...]  
> - name: mounting the volume in the fstab file  
> &nbsp;&nbsp;hosts: nfsgroup  
> &nbsp;&nbsp;gather\_facts: false  
> &nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: mounting the volume in the fstab file  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mount:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fstype: nfs  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opts:  
> "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dump: "0"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;passno: "0"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ var1[0] }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ var2[0] }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: mounted  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: "{{ inventory\_hostname }}"  
> ...

delegate\_to: "{{ inventory\_hostname }}" is redundant.

Try "with\_together"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#with-together](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-together)

- name: mounting the volume in the fstab file  
&nbsp;&nbsp;hosts: nfsgroup  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;become: yes  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: mounting the volume in the fstab file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mount:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fstype: nfs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item.2 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: mounted  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_together:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ var1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ var2 }}"

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 27, 2020, 8:01am UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/15 "2020-04-27T08:01:18Z")

</div>

Errata:

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 27, 2020, 12:35pm UTC](https://forum.ansible.com/t/need-to-pass-variable-and-hostname-to-a-playbook-from-csv-file/32495/16 "2020-04-27T12:35:50Z")

</div>

Awesome Vladimir, i just tried the solution that you have suggested. It’s working as per my requirement. Once again thank you so much.

Regards,  
Suresh
