execute roles more then one times (with_sequence ?)

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.

roles don't work with_<anything>, just do

- role1
- role2
- role2
...
- role3

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

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.

“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

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.

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?

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

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.

Or you can, but you don’t want to? :slight_smile:

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.

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.