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 <vbo...@gmail.com 
> <javascript:>> wrote:
>
>> On Wed, 18 Mar 2020 07:28:27 -0700 (PDT)
>> Pandu jh <jhp...@gmail.com <javascript:>> 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 ansible...@googlegroups.com <javascript:>.
>> 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 ansible-project+unsubscr...@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.

Reply via email to