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/CAHedzhKGSj%2Bdc%2BG_GCvC-xvVja2a4zt46-%2BXV-oHuW_wHTLLEw%40mail.gmail.com.

Reply via email to