Execute roles with grouped hosts

46 views
Skip to first unread message

Marko Božiković

unread,
Nov 12, 2019, 7:32:15 AM11/12/19
to ansible...@googlegroups.com
Hi all,

I'm trying to figure out a way to execute a role once per subgroup of hosts in
a single ansible-playbook run.

Here's an outline of my scenario: I want to configure multiple docker swarms.
My inventory looks something like this:

all:
  hosts:
    host-s1-1:
    host-s1-2:
    host-s2-1:
    host-s2-2:
  children:
    swarm-1:
      hosts:
        host-s1-1:
        host-s1-2:
    swarm-2:
      hosts:
        host-s2-1:
        host-s2-2:

    swarm:
      children:
        swarm-1:
        swarm-2:

I have a "docker-swarm" role which initialises a swarm, adds nodes, sets up
firewall rules, etc. My problem here is that swarm initialisation must be run
once per swarm, adding nodes is "add other nodes from my swarm group", etc.

If my play looks like this:
- hosts: swarm
  become: yes
  become_user: root
  become_method: sudo
  tasks:
    - name: Execute the docker role
      import_role:
        name: docker

I can run ansible-playbook once limiting it to swarm-1 group, and once
limiting it to swarm-2 group. If I don't explicitly limit it to one of the
swarm group's subgroups, the play will create one docker swarm.

Is there a way to write the play to execute the role once per swarm group's
child group? Maybe group hosts using a group variable value?

Thank you!

--
Marko Bozikovic
Senior Developer
Symplectic
Email: bo...@symplectic.co.uk

Kai Stian Olstad

unread,
Nov 12, 2019, 9:13:47 AM11/12/19
to ansible...@googlegroups.com
On Tue, Nov 12, 2019 at 01:32:01PM +0100, Marko Božiković wrote:
> Hi all,
>
> I'm trying to figure out a way to execute a role once per subgroup of hosts in
> a single ansible-playbook run.

Make two plays in you playbook file.


> If my play looks like this:
> - hosts: swarm
>   become: yes
>   become_user: root
>   become_method: sudo
>   tasks:
>     - name: Execute the docker role
>       import_role:
>         name: docker


- hosts: swarm-1
  become: yes
  become_user: root
  become_method: sudo
  tasks:
    - name: Execute the docker role
      import_role:
        name: docker

- hosts: swarm-2
  become: yes
  become_user: root
  become_method: sudo
  tasks:
    - name: Execute the docker role
      import_role:
        name: docker

--
Kai Stian Olstad

Marko Božiković

unread,
Nov 12, 2019, 10:52:06 AM11/12/19
to ansible...@googlegroups.com
On 12/11/2019 15:13, Kai Stian Olstad wrote:
> On Tue, Nov 12, 2019 at 01:32:01PM +0100, Marko Božiković wrote:
>> Hi all,
>>
>> I'm trying to figure out a way to execute a role once per subgroup of hosts in
>> a single ansible-playbook run.
> Make two plays in you playbook file.

Forgot to mention - I'd like to solve it for a general case, with N swarms :)

Kai Stian Olstad

unread,
Nov 12, 2019, 11:26:35 AM11/12/19
to ansible...@googlegroups.com
On Tue, Nov 12, 2019 at 04:51:53PM +0100, Marko Božiković wrote:
> On 12/11/2019 15:13, Kai Stian Olstad wrote:
> > On Tue, Nov 12, 2019 at 01:32:01PM +0100, Marko Božiković wrote:
> >> Hi all,
> >>
> >> I'm trying to figure out a way to execute a role once per subgroup of hosts in
> >> a single ansible-playbook run.
> > Make two plays in you playbook file.
>
> Forgot to mention - I'd like to solve it for a general case, with N swarms :)

Ansible is not design to do this, but switching to include_role and some
creative use of filters and test you can do something like this

- include_role:
name: docker
when: inventory_hostname in groups[item]
with_items: "{{ groups | dict2items | selectattr('key', 'match', 'swarm-') | list | map(attribute='key') | list }}"

--
Kai Stian Olstad

Marko Božiković

unread,
Nov 12, 2019, 12:22:29 PM11/12/19
to ansible...@googlegroups.com
Thank you very much, I'll see if I can make this work.
Reply all
Reply to author
Forward
0 new messages