Giovanni,
Thanks for joining in.
Having multiple plays in a playbook is obviously the easiest solution.
Especially when there's only a handful of combinations/groups. However,
what happens when you have 20 or 30 different combinations? And what
happens when the combinations of roles for a group frequently change? It
becomes very cumbersome to maintain the playbook and edit it on a per-run
basis.
Dynamic inventory allows for this sort of flexibility with assigning
variables to groups and/or hosts that define which roles are applicable.
It's difficult to account for these playbooks on the playbook side without
looping through roles. I'd like to do something like this:
---
- name: Apply roles to hosts
hosts: all
roles:
- { role: “{{item.value.rolename}}”, somevar: “{{item.value.someval}}”,
with_dict: host_roles }
Where each host has its own hostvars with something like what is shown
below:
---
host_roles:
foo:
rolename: foo
someval: 123
bar:
rolename: bar
someval: 456
Thanks
Chip
On Thursday, February 5, 2015 at 5:31:49 AM UTC-5, Giovanni Tirloni wrote:
>
> On Wed, 04 Feb 2015 16:38 -0800, Chip Selden <[email protected]
> <javascript:>>
> wrote:
> > Suraj,
> >
> > I've been trying to find a way to do this as well, with no luck so far.
> >
> > Ideally, I'd be able to use a loop (either with_items, or with_dict) to
> > run
> > multiple and differing roles on a per-host basis. But, alas, this is not
> > a
> > feature and from what I can find from older posts, will not be a
> feature.
> > Passing in a list variable to "roles:" is a good idea, but it doesn't
> > seem
> > like Ansible can use variables (even extra-vars from the
> ansible-playbook
> > command) to determine which roles are to be used.
>
> If I understand it, there is a need to run a playbook and it would apply
> a different set of roles to different set of roles, right?
>
> My suggestion is to define many plays per playbook. You'd not be
> defining a "roles" variable to be imported and, sure, there is more
> typing involved but it's clean and works:
>
>
> ---
> - name: Provision web layer
> hosts: webservers
> sudo: yes
> roles:
> - common
> - nginx
>
> - name: Provision db layer
> hosts: dbservers
> sudo: yes
> roles:
> - common
> - mariadb
>
>
> The reason defining a list somewhere else (say, group_vars/webservers)
> does not work is because it's becomes a dictionary ("web_roles -> {
> roleA, roleB }") and the "roles" parameter in a play expects a list.
>
> Just so I understand this better, would there be any issue with defining
> multi-play playbooks? What's exactly the use case in your scenario?
> People with access to change only vars files or perhaps the need to feed
> the role list dynamically? What are you trying to achieve? :)
>
> Giovanni
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/624565a5-25d0-47e0-ad6c-4f4646e34bd8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.