On Wed, 8 Apr 2020 19:55:04 +0530
Suresh Karpurapu <[email protected]> wrote:

> ... i would like to display all the entries.
> 
> # 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 }}"

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

-- 
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/20200408182343.2a8850b5%40gmail.com.

Attachment: pgpP8suPR54HY.pgp
Description: OpenPGP digital signature

Reply via email to