@Dick; I need more help please. 

This is what I did but it is still failing. 

   - name: Add hosts

       dest_ip: "{{ item.split('\t')[0] }}"

       file_dets: "{{ item.split('\t')[1] }}"
       ansible_host: localhost

       ansible_connection: local
     with_items: "{{ command_result.stdout_lines }}"


   - include: "{{ playbook_dir }}/gethosts.yml"




And below is my get_hosts.yml file

     add_host:
       name: {{ item }}
       groups: dest_nodes     
     with_items: "{{ dest_ip.split(',') }}"
     when: command_result.stdout != ""
     failed_when: command_result.stdout == ""

It is giving me errors while executing. 


On Tuesday, September 24, 2019 at 12:29:21 PM UTC+5:30, Dick Visser wrote:
>
> Instead of using add_host, use include_tasks to include a second task 
> file. In there, iterate over a “split by comma” list and adds hosts. 
> This gives you the second level of iteration. 
>
> Dick 
>
> On Tue, 24 Sep 2019 at 08:45, Mohtashim S <[email protected] 
> <javascript:>> wrote:
>
>> I have an ansible play that fetches multiline data from the database and 
>> registers it a variable "command_result"
>>
>> command_result variable has has data like below.
>>
>> host6,host5\targ3
>> host4,host3\targ1
>>
>> host1,host2,host5\targ2
>> .....
>> .....
>>
>>
>>
>> I need to loop through each line of command_result. Pick the host list 
>> for example host6,host5 and execute shell script using shell module by 
>> passing respective argument string i.e arg3.
>>
>> Likewise, the second shell script should run on  host4 & host3 and pass 
>> arg1 to the shell script. 
>>
>> The final shell script hould run on  host1, host2 & host5 and pass arg2 
>> to the shell script. 
>>
>>    - name: Add hosts
>>      add_host:
>>        name: "{{ item.split('\t')[0] }}"
>>        file_dets: "{{ item.split('\t')[1] }}"
>>
>>        ansible_host: localhost
>>
>>        ansible_connection: local
>>        groups: dest_nodes
>>      with_items: "{{ command_result.stdout_lines }}"
>>
>>
>> - hosts: dest_nodes
>>   gather_facts: false
>>   tasks:
>>     - debug:
>>         msg: Run the shell script with the arguments `{{ file_dets }}` 
>> here"
>>
>>
>> This works fine when there is a single host in  item.split('\t')[0]
>>
>> However, I do not know the approach when there are multiple hosts like 
>> host6,host5 for {{ item.split('\t')[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 [email protected] <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/ef6c8ef7-7462-4e05-8025-7d1f514329b7%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/ansible-project/ef6c8ef7-7462-4e05-8025-7d1f514329b7%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> -- 
> Sent from a mobile device - please excuse the brevity, spelling and 
> punctuation.
>

-- 
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/8f6fa0b3-4eae-43db-9a85-0bc82d4780ee%40googlegroups.com.

Reply via email to