How to trigger Ansible Play in a loop

40 views
Skip to first unread message

Mohtashim S

unread,
Sep 23, 2019, 8:50:46 AM9/23/19
to Ansible Project
command_result variable get database record while could be multi-line.

The below helps me loop over each record of the database.

   - debug:
       msg
: "This is a database line: {{ item }}"
     with_items
: "{{ command_result.stdout_lines }}"

What I now need is to trigger a fresh play like below for each loop iteration and its values.

- hosts: "{{ item.stdout.split('\t')[0] }}"
  tasks
:
   
- name: Check if reboot is required
      shell
: /home/ansible/scripts/check.sh "{{ item.stdout.split('\t')[1] }}"
     
register: output


   
- debug: var=output.stdout_lines


   
- add_host: name={{ item }}
               groups
=dest_nodes
               ansible_user
={{ USER }}
     with_items
: "{{ dest_ip.split(',') }}"

The item value will change on each loop and that will be fed to the play above. 

Can you please guide me how to achieve this ?

Stefan Hornburg (Racke)

unread,
Sep 23, 2019, 12:55:44 PM9/23/19
to ansible...@googlegroups.com
On 9/23/19 2:50 PM, Mohtashim S wrote:
> command_result variable get database record while could be multi-line.
>
> The below helps me loop over each record of the database.
>
> |
>    -debug:
>        msg:"This is a database line: {{ item }}"
>      with_items:"{{ command_result.stdout_lines }}"
> |
>
> What I now need is to trigger a fresh play like below for each loop iteration and its values.
>
> |
> -hosts:"{{ item.stdout.split('\t')[0] }}"
>   tasks:
>     -name:Checkifreboot isrequired
>       shell:/home/ansible/scripts/check.sh "{{ item.stdout.split('\t')[1] }}"
>       register:output
>
>
>     -debug:var=output.stdout_lines
>
>
>    -add_host:name={{item }}
>                groups=dest_nodes
>                ansible_user={{USER }}
>      with_items:"{{ dest_ip.split(',') }}"
> |
>
> The item value will change on each loop and that will be fed to the play above. 
>
> Can you please guide me how to achieve this ?

What do you mean with "trigger a fresh play" ?

Regards
Racke

>
> --
> 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-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/6a730169-fd2b-4a34-836e-2addb50d8490%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/6a730169-fd2b-4a34-836e-2addb50d8490%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc
Message has been deleted

Mohtashim S

unread,
Sep 23, 2019, 11:07:12 PM9/23/19
to Ansible Project
I mean that with every loop iteration the play with the shell script should be invoked with its respective details as supplied by the {{ item }}

So, if the database returns three records in "{{ command_result.stdout_lines }}" the shell script play should be invoked thrice with the details of each record in {{ item}} respectively.

For example: The database can return any number of rows and lets consider it returns three rows of type: <listofhosts>\t<somearguments>:

host5,host8\targ1
host6,host2\targ3
host9,host3,host4\targ4


What I need is the loop with_items: {{ command_result.stdout_lines }} would run three plays and each play to build dynamic host group of the host list for that run and its respective argument.

So, for the first run dynamic hosts group will be host5,host8 and the shell should get arg1

for second loop iteration dynamic hosts group will be host6,host2 and shell would get arg3

and so forth.

Hope this makes my requirement understood.

Can you please suggest.

Mohtashim S

unread,
Sep 24, 2019, 3:00:43 AM9/24/19
to Ansible Project
@Stefan, thank you for the reply. 

I have rephrased my query and posted to a new thread here: https://groups.google.com/forum/#!topic/ansible-project/tzFtb4MRZN0

Please suggest as feasible. 
Reply all
Reply to author
Forward
0 new messages