execute roles more then one times (with_sequence ?)

349 views
Skip to first unread message

Serkan C.

unread,
Jul 10, 2014, 6:08:26 AM7/10/14
to ansible...@googlegroups.com
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. 

Brian Coca

unread,
Jul 10, 2014, 7:09:44 AM7/10/14
to ansible...@googlegroups.com
roles don't work with_<anything>, just do 

- role1
- role2
- role2
...
- role3


--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Serkan C.

unread,
Jul 10, 2014, 7:18:27 AM7/10/14
to ansible...@googlegroups.com
I need to execute roles based on a variable.
Any idea?
Thanks.

Brian Coca

unread,
Jul 10, 2014, 7:26:49 AM7/10/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
Jul 10, 2014, 8:27:38 AM7/10/14
to ansible...@googlegroups.com
"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





On Thu, Jul 10, 2014 at 7:26 AM, Brian Coca <bria...@gmail.com> wrote:
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 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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CADn%2BHsz6YvdjD1QvrKAP-Pg_yE%2BOmeFKEgAeMGJXVVcy6_oZ-Q%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

James Cammarata

unread,
Jul 10, 2014, 10:54:05 AM7/10/14
to ansible...@googlegroups.com
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.


Serkan C.

unread,
Jul 10, 2014, 11:08:55 AM7/10/14
to ansible...@googlegroups.com
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?

James Cammarata

unread,
Jul 10, 2014, 3:04:24 PM7/10/14
to ansible...@googlegroups.com
In that case, you'd just pass in the variable as an integer and use that in with_sequence.


Serkan C.

unread,
Jul 11, 2014, 5:34:27 AM7/11/14
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
Jul 11, 2014, 6:29:16 PM7/11/14
to ansible...@googlegroups.com
Or you can, but you don't want to? :)




Serkan C.

unread,
Jul 14, 2014, 5:43:49 AM7/14/14
to ansible...@googlegroups.com
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. 

Michael DeHaan

unread,
Jul 14, 2014, 2:54:12 PM7/14/14
to ansible...@googlegroups.com
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.




Reply all
Reply to author
Forward
0 new messages