# Need help on json query call in ansible

**URL:** <https://forum.ansible.com/t/need-help-on-json-query-call-in-ansible/32692>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [April 15, 2020, 1:52pm UTC](https://forum.ansible.com/t/need-help-on-json-query-call-in-ansible/32692 "2020-04-15T13:52:04Z")\
**Posts on this page:** 4\
**Page:** 1

<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, 1:52pm UTC](https://forum.ansible.com/t/need-help-on-json-query-call-in-ansible/32692/1 "2020-04-15T13:52:04Z")

</div>

Hi Ansible Guru’s

I am using the CSV file as below for nfs volume migration as we have 1000 servers with 100 volumes. Hence, i would like to make to host inventory using add\_host file instead using inventory file. However, the loop is failing when i use add\_host module. Hence, i tried using json\_query as suggested by vladimar, and it is working for debug output but same is not working for mount module as i would like to update /etc/fstab and mount the volumes using this playbook. would you suggest the logic?

Playbook and CSV details:

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 15, 2020, 6:41pm UTC](https://forum.ansible.com/t/need-help-on-json-query-call-in-ansible/32692/2 "2020-04-15T18:41:52Z")

</div>

> Hi Ansible Guru's
> 
> I am using the CSV file as below for nfs volume migration as we have 1000 servers with 100 volumes. Hence, i would like  
> to make to host inventory using add\_host file instead using inventory file. However, the loop is failing when i use  
> add\_host module. Hence, i tried using json\_query as suggested by vladimar, and it is working for debug output but same  
> is not working for mount module as i would like to update /etc/fstab and mount the volumes using this playbook. would  
> you suggest the logic?

var1 and var2 are lists, and not strings which are expected by the mount module.

Try var1[0] and var2[0] instead.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<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 16, 2020, 10:40am UTC](https://forum.ansible.com/t/need-help-on-json-query-call-in-ansible/32692/3 "2020-04-16T10:40:04Z")

</div>

Thanks Racke, it’s working for if host has single volume but same is failing if the host has multiple volumes. Basically, loop is getting failed. Please suggest if there any alternative ways

# cat mounts.csv

host,remote\_path,mnt\_path,python  
host1,nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01,/myasup/stg/data,/usr/bin/python2.6  
host1,nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01,/myasup/stg/app,/usr/bin/python2.6  
host2,nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01,/myasup/stg/data,/usr/bin/python2.6  
host2,nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01,/myasup/stg/app,/usr/bin/python2.6

Result:

TASK [Grouping host and volume information] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /suresh/suresh\_playbooks/mounts.yml:12  
creating host via ‘add\_host’: hostname=host1  
changed: [localhost] =\> (item=[u’host1’, [{u’python’: u’/usr/bin/python2.6’, u’host’: u’host1’, u’mnt\_path’: u’/myasup/stg/data’, u’remote\_path’: u’nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01’}, {u’python’: u’/usr/bin/python2.6’, u’host’: u’host1’, u’mnt\_path’: u’/myasup/stg/app’, u’remote\_path’: u’nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01’}]]) =\> {  
“add\_host”: {  
“groups”: [  
“nfsgroup”  
],  
“host\_name”: “host1”,  
“host\_vars”: {  
“ansible\_python\_interpreter”: “/usr/bin/python2.6”,  
“var1”: [  
“nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”,  
“nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01”  
],  
“var2”: [  
“/myasup/stg/data”,  
“/myasup/stg/app”  
]  
}  
},  
“ansible\_loop\_var”: “item”,  
“changed”: true,  
“item”: [  
“host1”,  
[  
{  
“host”: “host1”,  
“mnt\_path”: “/myasup/stg/data”,  
“python”: “/usr/bin/python2.6”,  
“remote\_path”: “nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”  
},  
{  
“host”: “host1”,  
“mnt\_path”: “/myasup/stg/app”,  
“python”: “/usr/bin/python2.6”,  
“remote\_path”: “nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01”  
}  
]  
]  
}  
creating host via ‘add\_host’: hostname=host2  
changed: [localhost] =\> (item=[u’host2’, [{u’python’: u’/usr/bin/python2.6’, u’host’: u’host2’, u’mnt\_path’: u’/myasup/stg/data’, u’remote\_path’: u’nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01’}, {u’python’: u’/usr/bin/python2.6’, u’host’: u’host2’, u’mnt\_path’: u’/myasup/stg/app’, u’remote\_path’: u’nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01’}]]) =\> {  
“add\_host”: {  
“groups”: [  
“nfsgroup”  
],  
“host\_name”: “host2”,  
“host\_vars”: {  
“ansible\_python\_interpreter”: “/usr/bin/python2.6”,  
“var1”: [  
“nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”,  
“nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01”  
],  
“var2”: [  
“/myasup/stg/data”,  
“/myasup/stg/app”  
]  
}  
},  
“ansible\_loop\_var”: “item”,  
“changed”: true,  
“item”: [  
“host2”,  
[  
{  
“host”: “host2”,  
“mnt\_path”: “/myasup/stg/data”,  
“python”: “/usr/bin/python2.6”,  
“remote\_path”: “nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”  
},  
{  
“host”: “host2”,  
“mnt\_path”: “/myasup/stg/app”,  
“python”: “/usr/bin/python2.6”,  
“remote\_path”: “nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01”  
}  
]  
]  
}  
META: ran handlers  
META: ran handlers

TASK [debug output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /suresh/suresh\_playbooks/mounts.yml:25  
ok: [host1] =\> {  
“msg”: [  
“host1”,  
[  
“nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”,  
“nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01”  
],  
[  
“/myasup/stg/data”,  
“/myasup/stg/app”  
]  
]  
}  
ok: [host2] =\> {  
“msg”: [  
“host2”,  
[  
“nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”,  
“nfsflr02:/volahcstg\_www\_masup\_stg\_app\_01”  
],  
[  
“/myasup/stg/data”,  
“/myasup/stg/app”  
]  
]  
}

TASK [mounting the volume in the fstab file] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [host1 → host1] =\> {  
“changed”: true,  
“dump”: “0”,  
“fstab”: “/etc/fstab”,  
“fstype”: “nfs”,  
“invocation”: {  
“module\_args”: {  
“backup”: false,  
“boot”: true,  
“dump”: “0”,  
“fstab”: null,  
“fstype”: “nfs”,  
“opts”: “vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600”,  
“passno”: “0”,  
“path”: “/myasup/stg/data”,  
“src”: “nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”,  
“state”: “mounted”  
}  
},  
“name”: “/myasup/stg/data”,  
“opts”: “vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600”,  
“passno”: “0”,  
“src”: “nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”  
}  
 (0, ‘’, ‘’)  
changed: [host2 → host2] =\> {  
“changed”: true,  
“dump”: “0”,  
“fstab”: “/etc/fstab”,  
“fstype”: “nfs”,  
“invocation”: {  
“module\_args”: {  
“backup”: false,  
“boot”: true,  
“dump”: “0”,  
“fstab”: null,  
“fstype”: “nfs”,  
“opts”: “vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600”,  
“passno”: “0”,  
“path”: “/myasup/stg/data”,  
“src”: “nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”,  
“state”: “mounted”  
}  
},  
“name”: “/myasup/stg/data”,  
“opts”: “vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600”,  
“passno”: “0”,  
“src”: “nfsflr01:/volahcstg\_www\_masup\_stg\_data\_01”  
}  
META: ran handlers  
META: ran handlers

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0  
host1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0  
host2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

---

<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 17, 2020, 8:07am UTC](https://forum.ansible.com/t/need-help-on-json-query-call-in-ansible/32692/4 "2020-04-17T08:07:23Z")

</div>

Can anyone help me on this request or please suggest if there any alternative solution for my requirement?

Regards,  
Suresh
