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:
=========================
# cat mounts.csv
host,remote_path,mnt_path,python
host1,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
host2,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
# cat mounts.yml
---
- name: mount the nfsshare in client side
hosts: localhost
gather_facts: false
become: yes
tasks:
- name: reading volume info from csv
read_csv:
path: "{{ playbook_dir }}/mounts.csv"
register: sources
- name: Grouping host and volume information
add_host:
name: "{{ item.0 }}"
groups: nfsgroup
var1: "{{ item.1|json_query('[].remote_path') }}"
var2: "{{ item.1|json_query('[].mnt_path') }}"
ansible_python_interpreter: "{{ item.1.0.python }}"
loop: "{{ sources.list|groupby('host') }}"
- name: list the volumes
hosts: nfsgroup
become: yes
gather_facts: false
tasks:
- name: debug output
debug:
msg:
- "{{ inventory_hostname }}"
- "{{ var1 }}"
- "{{ var2 }}"
- name: mounting the volume in the fstab file
hosts: nfsgroup
gather_facts: false
become: yes
tasks:
- name: mounting the volume in the fstab file
mount:
fstype: nfs
opts: "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp"
dump: "0"
passno: "0"
src: "{{ var1 }}"
path: "{{ var2 }}"
state: mounted
delegate_to: "{{ inventory_hostname }}"
...
================================================================================================================================================
Playbook Verbose Output:
================================================================================================================================================
TASK [reading volume info from csv] *************************************************************************************************************************************
task path: /suresh/suresh_playbooks/mounts.yml:7
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532 `" && echo ansible-tmp-1586795492.17-47707210108532="` echo /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/files/read_csv.py
<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-26007iAx4cI/tmpxiOrdU TO /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/AnsiballZ_read_csv.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/ /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/AnsiballZ_read_csv.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/AnsiballZ_read_csv.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1586795492.17-47707210108532/ > /dev/null 2>&1 && sleep 0'
ok: [localhost] => {
"changed": false,
"dict": {},
"invocation": {
"module_args": {
"delimiter": null,
"dialect": "excel",
"fieldnames": null,
"key": null,
"path": "/suresh/suresh_playbooks/mounts.csv",
"skipinitialspace": null,
"strict": null,
"unique": true
}
},
"list": [
{
"host": "host1",
"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/data",
"python": "/usr/bin/python2.6",
"remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
}
]
}
TASK [Grouping host and volume information] *****************************************************************************************************************************
task path: /suresh/suresh_playbooks/mounts.yml:11
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'}]]) => {
"add_host": {
"groups": [
"nfsgroup"
],
"host_name": "host1",
"host_vars": {
"ansible_python_interpreter": "/usr/bin/python2.6",
"var1": [
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
"var2": [
"/myasup/stg/data"
]
}
},
"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"
}
]
]
}
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'}]]) => {
"add_host": {
"groups": [
"nfsgroup"
],
"host_name": "host2",
"host_vars": {
"ansible_python_interpreter": "/usr/bin/python2.6",
"var1": [
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
"var2": [
"/myasup/stg/data"
]
}
},
"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"
}
]
]
}
META: ran handlers
META: ran handlers
PLAY [list the volumes] *************************************************************************************************************************************************
META: ran handlers
TASK [debug output] *****************************************************************************************************************************************************
task path: /suresh/suresh_playbooks/mounts.yml:24
ok: [host2] => {
"msg": [
"host2",
[
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
[
"/myasup/stg/data"
]
]
}
ok: [host1] => {
"msg": [
"host1",
[
"nfsflr01:/volahcstg_www_masup_stg_data_01"
],
[
"/myasup/stg/data"
]
]
}
META: ran handlers
META: ran handlers
PLAY [mounting the volume in the fstab file] ****************************************************************************************************************************
META: ran handlers
TASK [mounting the volume in the fstab file] *******
<host2> (0, '', '')
fatal: [host2]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"backup": false,
"boot": true,
"dump": "0",
"fstab": null,
"fstype": "nfs",
"opts": "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp",
"passno": "0",
"path": "['/myasup/stg/data']",
"src": "['nfsflr01:/volahcstg_www_masup_stg_data_01']",
"state": "mounted"
}
},
"msg": "Error mounting ['/myasup/stg/data']: mount: can't get address for ['nfsflr01\n"
fatal: [host1]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"backup": false,
"boot": true,
"dump": "0",
"fstab": null,
"fstype": "nfs",
"opts": "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp",
"passno": "0",
"path": "['/myasup/stg/data']",
"src": "['nfsflr01:/volahcstg_www_masup_stg_data_01']",
"state": "mounted"
}
},
"msg": "Error mounting ['/myasup/stg/data']: mount: can't get address for ['nfsflr01\n"
}
PLAY RECAP **************************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host1 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
host2 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Result on target servers fstab file
===================================
# grep 'nfsflr01:/volahcstg_www_masup_stg_data_01' /etc/fstab
nfsflr01:/volahcstg_www_masup_stg_data_01 /myasup/stg/data nfs vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600 0 0
['nfsflr01:/volahcstg_www_masup_stg_data_01',\040'nasdurahc01spd02:/volahcstg_www_masup_stg_app_01'] ['/myasup/stg/data',\040'/myasup/stg/app'] nfs rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp 0 0