# How to trigger Ansible Play in a loop

**URL:** <https://forum.ansible.com/t/how-to-trigger-ansible-play-in-a-loop/31338>\
**Category:** Ansible Project\
**Created:** [September 23, 2019, 12:50pm UTC](https://forum.ansible.com/t/how-to-trigger-ansible-play-in-a-loop/31338 "2019-09-23T12:50:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 23, 2019, 12:50pm UTC](https://forum.ansible.com/t/how-to-trigger-ansible-play-in-a-loop/31338/1 "2019-09-23T12:50:46Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 23, 2019, 4:55pm UTC](https://forum.ansible.com/t/how-to-trigger-ansible-play-in-a-loop/31338/2 "2019-09-23T16:55:22Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 24, 2019, 3:07am UTC](https://forum.ansible.com/t/how-to-trigger-ansible-play-in-a-loop/31338/3 "2019-09-24T03:07:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 24, 2019, 7:00am UTC](https://forum.ansible.com/t/how-to-trigger-ansible-play-in-a-loop/31338/4 "2019-09-24T07:00:42Z")

</div>

@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](https://groups.google.com/forum/#!topic/ansible-project/tzFtb4MRZN0)

Please suggest as feasible.
