# Help executing tasks serially over a dynamic set of groups

**URL:** <https://forum.ansible.com/t/help-executing-tasks-serially-over-a-dynamic-set-of-groups/14401>\
**Category:** Ansible Project\
**Created:** [January 22, 2014, 10:26pm UTC](https://forum.ansible.com/t/help-executing-tasks-serially-over-a-dynamic-set-of-groups/14401 "2014-01-22T22:26:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [January 22, 2014, 10:26pm UTC](https://forum.ansible.com/t/help-executing-tasks-serially-over-a-dynamic-set-of-groups/14401/1 "2014-01-22T22:26:18Z")

</div>

Here's a fun scenario.

Our inventory is divided up into multiple environments, that is when we execute our inventory plugin, we provide an ENV\_NAME environment variable to tell the plugin which environment to load.

Within each environment there are a number of logical groupings, we'll call these "cells". Each environment has 1 or more cell, and they get added semi-frequently so there isn't necessarily a hard coded list of them (although it is discoverable at run-time). My inventory plugin creates a group for each of those cells, that has other child-groups within.

My need is to run a couple tasks. A torrent task and a stop torrent task. These tasks need to run in parallel within the cell, but serially through the list of cells. That is I have to torrent and then stop torrenting in cell1 before doing anything in cell2, and then cell3, etc...

I'm looking for ways to express this in a playbook and falling short. I know I could use a shell script to loop over calling ansible-playbook providing a different --limit value each time but that feels inelegant.

So I ask you, fellow Ansible users, do you have any ideas?

-jlk

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [January 23, 2014, 8:05am UTC](https://forum.ansible.com/t/help-executing-tasks-serially-over-a-dynamic-set-of-groups/14401/2 "2014-01-23T08:05:33Z")

</div>

Just a (dumb?) idea ...

Not sure it would be more elegant but having ansible-playbook calling  
ansible-playbook ....  
That is the top level playbook do all the inventory stuff to calculate  
cell1, cell2 , .. as host groups

and then having a loop task that call ansible-playbook (via shell  
command) from the local\_host doing  
the effective work and passing the {{ current\_cell }} as a set of host  
for a play in the second level playbook

so a way to simulate a loop over a play (the host mapping layer) with  
the capability to change the play  
parameters (hosts in your case)  
Not sure Michael would like to see this being materialized as a  
'playbook' module 🙂

my half cent.

--Phil

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 23, 2014, 1:18pm UTC](https://forum.ansible.com/t/help-executing-tasks-serially-over-a-dynamic-set-of-groups/14401/3 "2014-01-23T13:18:50Z")

</div>

AWX has the ability to run multiple jobs in parallel and still record all log data centrally.

It has a _VERY_ good REST API for doing this.

[http://www.ansibleworks.com/ansibleworks-awx/](http://www.ansibleworks.com/ansibleworks-awx/)

Ansible’s CLI doesn’t really make sense to have the CLI do this IMHO, because the output would be a mess with so many different things happening in parallel, so it does one play at a time normally.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 23, 2014, 6:57pm UTC](https://forum.ansible.com/t/help-executing-tasks-serially-over-a-dynamic-set-of-groups/14401/4 "2014-01-23T18:57:14Z")

</div>

I thought over this yesterday evening, and was trying to figure out a way to do it with playbooks, and could not come to anything that made sense. But I realized I was only looking at the problem from 1 angle. ansible provides more than ansible-playbook, and in fact I think the only way to achieve this is to use the Ansible Python API, which ansible-playbook utilizes.

You could, probably with minimal changes, convert ansible-playbook, to loop it’s execution of the playbook multiple times over the groups of an inventory.

Just some thoughts, but I am sure it could be done somewhat easily. However without some additional work to log the tasks and such, it will really just look like you have run ansible-playbook multiple times.
