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


On Wed, Apr 8, 2020 at 8:48 PM Pandu jh <[email protected]> wrote:

>  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
>
>
>
> On Wednesday, 8 April 2020 20:07:27 UTC+5:30, Suresh Karpurapu wrote:
>>
>> 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
>>
>>
>> On Sat, Mar 21, 2020 at 3:58 AM Vladimir Botka <[email protected]> wrote:
>>
>>> On Wed, 18 Mar 2020 07:28:27 -0700 (PDT)
>>> Pandu jh <[email protected]> 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
>>>
>>> --
>>> 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 [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ansible-project/20200320232753.5d772d73%40gmail.com
>>> .
>>>
>> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/7b9ab6aa-379a-4f31-8fd4-577a5c52e654%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/7b9ab6aa-379a-4f31-8fd4-577a5c52e654%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHedzhLrLygPUsDi-qi8Rpn0tbR5xd1rg1LNAPyXbnSKRkB3RQ%40mail.gmail.com.

Reply via email to