# execute roles more then one times (with\_sequence ?)

**URL:** <https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641>\
**Category:** Ansible Project\
**Created:** [July 10, 2014, 10:08am UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641 "2014-07-10T10:08:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serkan\_C](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Serkan\_C](https://forum.ansible.com/u/Serkan_C)\
**Post date:** [July 10, 2014, 10:08am UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/1 "2014-07-10T10:08:26Z")

</div>

Hi All,  
I would like to execute some rules more than one time at the playbook, i tried to use;  
with\_sequence: count=2  
but  
ERROR: with\_sequence is not a legal parameter in an Ansible Playbook

I would like to do something like that;

…  
roles:

- role1
- role2  
with\_sequence: count={{count}}
- role3

I can not decribe with\_sequence inside the roles because it has lots of task.

Any idea?

Thanks.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 10, 2014, 11:09am UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/2 "2014-07-10T11:09:40Z")

</div>

roles don't work with\_\<anything\>, just do

- role1  
- role2  
- role2  
...  
- role3

---

<div class="post-metadata">

**Author:** ![Serkan\_C](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Serkan\_C](https://forum.ansible.com/u/Serkan_C)\
**Post date:** [July 10, 2014, 11:18am UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/3 "2014-07-10T11:18:27Z")

</div>

I need to execute roles based on a variable.  
Any idea?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 10, 2014, 11:26am UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/4 "2014-07-10T11:26:46Z")

</div>

you cannot execute a role conditionally, you CAN pass a condition to every task in a role.

role and include directives are basically ‘preprocessing macros’ they are used to include other files into main play.  
​

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 10, 2014, 12:27pm UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/5 "2014-07-10T12:27:34Z")

</div>

“you cannot execute a role conditionally, you CAN pass a condition to every task in a role.”

True!

- { role: asdf, when: foo == ‘asdf’ }

Another good trick is group\_by is a great way to create a small group of hosts where a given condition is true or false, and then you just apply that role to that group.

- hosts: all  
tasks:
- group\_by: key=foo\_{{ foo }}

# talk to all hosts where the value of foo is asdf

- hosts: foo\_asdf  
roles:
- asdf

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [July 10, 2014, 2:54pm UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/6 "2014-07-10T14:54:01Z")

</div>

The other way we typically recommend doing this is to pass the array of whatever as a role parameter. For example:

roles:

- { role: foo, list\_of\_whatever: […] }

and then loop over that list\_of\_whatever variable inside the roles tasks.

---

<div class="post-metadata">

**Author:** ![Serkan\_C](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Serkan\_C](https://forum.ansible.com/u/Serkan_C)\
**Post date:** [July 10, 2014, 3:08pm UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/7 "2014-07-10T15:08:55Z")

</div>

Thanks for suggestion, it looks like i can find a solution with that method.  
I would like to pass just count, a number, how can i do that?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [July 10, 2014, 7:04pm UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/8 "2014-07-10T19:04:18Z")

</div>

In that case, you’d just pass in the variable as an integer and use that in with\_sequence.

---

<div class="post-metadata">

**Author:** ![Serkan\_C](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Serkan\_C](https://forum.ansible.com/u/Serkan_C)\
**Post date:** [July 11, 2014, 9:34am UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/9 "2014-07-11T09:34:26Z")

</div>

I already use this method to pass variable to roles but in this case role1 has 15 tasks, i cannot iterate all of them one by one.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 11, 2014, 10:29pm UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/10 "2014-07-11T22:29:11Z")

</div>

Or you can, but you don’t want to? 🙂

---

<div class="post-metadata">

**Author:** ![Serkan\_C](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Serkan\_C](https://forum.ansible.com/u/Serkan_C)\
**Post date:** [July 14, 2014, 9:43am UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/11 "2014-07-14T09:43:49Z")

</div>

Yes i can do that, for example;

- name: do something1  
module: parameters  
with\_sequence: start=0 end={{count}}

- name: do something2  
module: parameters  
with\_sequence: start=0 end={{count}}

- name: do something3  
module: parameters  
with\_sequence: start=0 end={{count}}

- name: do something4  
module: parameters  
with\_sequence: start=0 end={{count}}

- name: do something5  
module: parameters  
with\_sequence: start=0 end={{count}}

and i don’t want to do that, because it’s not a clever way to iterate something.  
It should be that difficult to iterate a role, that’s my point.  
Thank you for suggestions.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 14, 2014, 6:54pm UTC](https://forum.ansible.com/t/execute-roles-more-then-one-times-with-sequence/15641/12 "2014-07-14T18:54:08Z")

</div>

Right, role iteration just can’t happen, because what happens is people will want different values to iterate for different hosts, and the way Ansible is built all task definitions in a play go to all hosts, and then those hosts evaluate conditionals.

What you ask would involve auto-converting some loops to item loops, and some item loops to nested loops, which is difficult to do in an automagical way that is intuitive.

As such, this is just somewhat hard to make a thing. If we made this a thing, ansible output wouldn’t make sense as it tries to aggregrate things for multiple hosts.
