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

543 views
Skip to first unread message

Pandu jh

unread,
Mar 18, 2020, 10:28:27 AM3/18/20
to Ansible Project
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

J

unread,
Mar 20, 2020, 3:05:05 PM3/20/20
to Ansible Project

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.

 

---
- name: read items from csv and store as vars
  hosts: localhost
  become: false
  tasks:
    - name: read_csv
      read_csv:
        path: ip.csv
      register: ip
    - name: print csv items
      debug:
        msg: "{{ip.list.2.ip}} {{ip.list.2.username}} {{ip.list.2.host}}"
    - name:
      shell: "ssh {{ip.list.2.username}}@{{ip.list.2.ip}} 'ls -la'"

Vladimir Botka

unread,
Mar 20, 2020, 6:28:46 PM3/20/20
to Pandu jh, ansible...@googlegroups.com
On Wed, 18 Mar 2020 07:28:27 -0700 (PDT)
Pandu jh <jhp...@gmail.com> wrote:

> 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

shell> cat playbook.yml
- hosts: localhost
gather_facts: false
tasks:
- read_csv:
path: AnsibleTest.csv
register: myhosts
- add_host:
name: '{{ item.ip }}'
groups: test_01
var1: "{{ item.var1 }}"
var2: "{{ item.var2 }}"
loop: "{{ myhosts.list }}"

- hosts: test_01
gather_facts: false
tasks:
- debug:
msg:
- "{{ inventory_hostname }}"
- "{{ var1 }}"
- "{{ var2 }}"

shell> ansible-playbook playbook.yml
...
ok: [10.1.1.1] => {
"msg": [
"10.1.1.1",
"rchantif1",
"mechlab"
]
}
ok: [10.1.1.2] => {
"msg": [
"10.1.1.2",
"rchans01",
"contlab"
]
}

HTH,

-vlado

Suresh Karpurapu

unread,
Apr 8, 2020, 10:37:27 AM4/8/20
to ansible...@googlegroups.com, vbo...@gmail.com, Pandu jh
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
---
- 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
      delegate_to: localhost
    - name: Grouping host and volume information
      add_host:
        name: "{{ item.host }}"
        groups: nfsgroup
        var1: "{{ item.remote_path }}"
        var2: "{{ item.mnt_path }}"
      loop: "{{ sources.list }}"
- name: list volume details
  hosts: nfsgroup
  become: yes

  gather_facts: false
  tasks:
    - debug:
        msg:
          - "{{ inventory_hostname }}"
          - "{{ var1 }}"
          - "{{ var2 }}"
...

Output:
======
PLAY [list volume details] **********************************************************************************************************************************************

TASK [debug] ************************************************************************************************************************************************************
ok: [host1] => {
    "msg": [
        "host1",
        "nfsvol3",
        "mount3"
    ]
}
ok: [host2] => {
    "msg": [
        "host2",
        "nfsvol3",
        "mount3"
    ]
}

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=0    skipped=0    rescued=0    ignored=0
host2   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20200320232753.5d772d73%40gmail.com.
Message has been deleted

Pandu jh

unread,
Apr 8, 2020, 11:18:05 AM4/8/20
to Ansible Project
 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
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Suresh Karpurapu

unread,
Apr 8, 2020, 11:19:53 AM4/8/20
to ansible...@googlegroups.com
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



On Wed, Apr 8, 2020 at 8:09 PM Pandu jh <jhp...@gmail.com> wrote:
Can you try to format your csv data to inventory as below.

 # cat mounts

host,remote_path,mnt_path
host1,nfsvol1,mount1
host1,nfsvol2,mount2
host1,nfsvol3,mount3
host2,nfsvol1,mount1
host2,nfsvol2,mount2
host2,nfsvol3,mount3

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c586db4f-82f6-4560-b567-9ca1f125e342%40googlegroups.com.

Suresh Karpurapu

unread,
Apr 8, 2020, 11:32:26 AM4/8/20
to ansible...@googlegroups.com
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
---

  - name: reading volume info from csv
    read_csv:
      path: "{{ playbook_dir }}/roles/volmount/files/sources.csv"
    register: sources
    delegate_to: localhost
  - name: check the volume in fstab
    shell: "grep {{ source.remote_path }} /etc/fstab|awk '{print $4}'"
    loop: "{{ sources.list }}"
    loop_control:
      loop_var: source
    register: source
  - name: mounting the volume in the fstab file
    mount:
      fstype: nfs
      opts: "{{ item.stdout_lines[0] }}"
      dump: "{{ fs_dump }}"
      passno: "{{ fsck_opt }}"
      src: "{{ item.source.remote_path }}"
      path: "{{ item.source.mnt_path }}"
      state: mounted
    when: item.stdout != ""
    loop: "{{ source.results }}"
...
# cat nfsvolmigration/roles/volunmount/tasks/volunmount.yml
---
  - name: "reading volume info from csv file"
    read_csv:
      path: "{{ playbook_dir }}/roles/volunmount/files/sources.csv"
    register: sources
    delegate_to: localhost
  - name: check if the volumes are mounted
    shell: mount -t nfs | grep "{{ source.remote_path }}" | awk '{print $1}'
    args:
      warn: false
    loop: "{{ sources.list }}"
    loop_control:
      loop_var: source
    register: mounts
  - name: unmount nfs_shares
    shell: /bin/umount -lf {{ item.source.mnt_path }}
    when: item.stdout != ""
    loop: "{{ mounts.results }}"
  - name: unmount nfs_shares if there any
    mount:
      path: "{{ item.source.mnt_path }}"
      src: "{{ item.source.remote_path }}"
      state: unmounted
    when: item.stdout != ""
    loop: "{{ mounts.results }}"
...

Regards,
Suresh


To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7b9ab6aa-379a-4f31-8fd4-577a5c52e654%40googlegroups.com.

Vladimir Botka

unread,
Apr 8, 2020, 12:24:09 PM4/8/20
to Suresh Karpurapu, ansible...@googlegroups.com, Pandu jh
On Wed, 8 Apr 2020 19:55:04 +0530
Suresh Karpurapu <karpurap...@gmail.com> wrote:

> ... i would like to display all the entries.
Use "groupby" filter. For example
https://jinja.palletsprojects.com/en/master/templates/#groupby

add_host:
name: "{{ item.0 }}"
groups: nfsgroup
var1: "{{ item.1|json_query('[].remote_path') }}"
var2: "{{ item.1|json_query('[].mnt_path') }}"
loop: "{{ sources.list|groupby('host') }}"

> - name: list volume details
> hosts: nfsgroup
> become: yes
> gather_facts: false
> tasks:
> - debug:
> msg:
> - "{{ inventory_hostname }}"
> - "{{ var1 }}"
> - "{{ var2 }}"

You should receive lists of "remote_path" and "mnt_path" in var1 and var2
respectively.

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

HTH,

-vlado

Suresh Karpurapu

unread,
Apr 8, 2020, 1:46:06 PM4/8/20
to Vladimir Botka, ansible...@googlegroups.com, Pandu jh
awesome, it really helped for my requirement. thank you so much Vladimir,

Regards,
Suresh

Suresh Karpurapu

unread,
Apr 9, 2020, 8:17:11 AM4/9/20
to Vladimir Botka, ansible...@googlegroups.com, Pandu jh
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

Vladimir Botka

unread,
Apr 9, 2020, 11:21:26 AM4/9/20
to Suresh Karpurapu, ansible...@googlegroups.com, Pandu jh
Hi Suresh,

On Thu, 9 Apr 2020 17:46:42 +0530
Suresh Karpurapu <karpurap...@gmail.com> wrote:

> 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?

shell> 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

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') }}"

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

HTH,
vlado

> >> On Wed, 8 Apr 2020 19:55:04 +0530
> >> Suresh Karpurapu <karpurap...@gmail.com> wrote:
> >> >
> >> > # 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
> >> >
> >> > - 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
> >> > delegate_to: localhost
> >> > - name: Grouping host and volume information
> >>
> >> add_host:

Suresh Karpurapu

unread,
Apr 14, 2020, 10:03:29 AM4/14/20
to Vladimir Botka, ansible...@googlegroups.com, Pandu jh
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

Suresh Karpurapu

unread,
Apr 15, 2020, 1:33:42 AM4/15/20
to Vladimir Botka, ansible...@googlegroups.com, Pandu jh
Would anyone please help me with this?

Regards,
Suresh

Vladimir Botka

unread,
Apr 27, 2020, 3:46:38 AM4/27/20
to Suresh Karpurapu, ansible...@googlegroups.com
On Mon, 20 Apr 2020 21:58:34 +0530
Suresh Karpurapu <karpurap...@gmail.com> wrote:

> ... 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
> 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[0] }}"
> path: "{{ var2[0] }}"
> state: mounted
> 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

- 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
...
src: "{{ item.1 }}"
path: "{{ item.2 }}"
state: mounted
with_together:
- "{{ var1 }}"
- "{{ var2 }}"


> *Result:*
> TASK [list the volumes]
> ok: [host1] => {
> "msg": [
> "host1",
> [
> "nfsflr01:/volahcstg_www_masup_stg_data_01",
> "nfsflr02:/volahcstg_www_masup_stg_app_01"
> ],
> [
> "/myasup/stg/data",
> "/myasup/stg/app"
> ]
> ]

Vladimir Botka

unread,
Apr 27, 2020, 4:01:37 AM4/27/20
to Suresh Karpurapu, ansible...@googlegroups.com
Errata:

> - name: mounting the volume in the fstab file
> mount:
> fstype: nfs
> ...
> src: "{{ item.0 }}"
> path: "{{ item.1 }}"

Suresh Karpurapu

unread,
Apr 27, 2020, 8:36:24 AM4/27/20
to Vladimir Botka, ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages