# Execute roles with grouped hosts

**URL:** <https://forum.ansible.com/t/execute-roles-with-grouped-hosts/31642>\
**Category:** Ansible Project\
**Created:** [November 12, 2019, 12:32pm UTC](https://forum.ansible.com/t/execute-roles-with-grouped-hosts/31642 "2019-11-12T12:32:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bozho](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bozho/32/10430_2.png) [@bozho](https://forum.ansible.com/u/bozho)\
**Post date:** [November 12, 2019, 12:32pm UTC](https://forum.ansible.com/t/execute-roles-with-grouped-hosts/31642/1 "2019-11-12T12:32:01Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 12, 2019, 2:13pm UTC](https://forum.ansible.com/t/execute-roles-with-grouped-hosts/31642/2 "2019-11-12T14:13:35Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![bozho](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bozho/32/10430_2.png) [@bozho](https://forum.ansible.com/u/bozho)\
**Post date:** [November 12, 2019, 3:51pm UTC](https://forum.ansible.com/t/execute-roles-with-grouped-hosts/31642/3 "2019-11-12T15:51:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 12, 2019, 4:26pm UTC](https://forum.ansible.com/t/execute-roles-with-grouped-hosts/31642/4 "2019-11-12T16:26:19Z")

</div>

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:  
&nbsp;&nbsp;&nbsp;&nbsp;name: docker  
&nbsp;&nbsp;when: inventory\_hostname in groups[item]  
&nbsp;&nbsp;with\_items: "{{ groups | dict2items | selectattr('key', 'match', 'swarm-') | list | map(attribute='key') | list }}"

---

<div class="post-metadata">

**Author:** ![bozho](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bozho/32/10430_2.png) [@bozho](https://forum.ansible.com/u/bozho)\
**Post date:** [November 12, 2019, 5:22pm UTC](https://forum.ansible.com/t/execute-roles-with-grouped-hosts/31642/5 "2019-11-12T17:22:16Z")

</div>

Thank you very much, I'll see if I can make this work.
