# A host appears in more than one group, and both groups have the same tasks in them; any way to run tasks once?

**URL:** <https://forum.ansible.com/t/a-host-appears-in-more-than-one-group-and-both-groups-have-the-same-tasks-in-them-any-way-to-run-tasks-once/17489>\
**Category:** Ansible Project\
**Created:** [February 10, 2015, 1:47pm UTC](https://forum.ansible.com/t/a-host-appears-in-more-than-one-group-and-both-groups-have-the-same-tasks-in-them-any-way-to-run-tasks-once/17489 "2015-02-10T13:47:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asfand\_Yar\_Qazi](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Asfand\_Yar\_Qazi](https://forum.ansible.com/u/Asfand_Yar_Qazi)\
**Post date:** [February 10, 2015, 1:47pm UTC](https://forum.ansible.com/t/a-host-appears-in-more-than-one-group-and-both-groups-have-the-same-tasks-in-them-any-way-to-run-tasks-once/17489/1 "2015-02-10T13:47:41Z")

</div>

Hi,

If anyone wants Stack Exchange brownie points, I have asked this question on Server fault here:

[http://serverfault.com/questions/666457/ansible-a-host-appears-in-more-than-one-group-and-both-groups-have-the-same-ta](http://serverfault.com/questions/666457/ansible-a-host-appears-in-more-than-one-group-and-both-groups-have-the-same-ta)

I have a playbook that looks something like this:

```auto
---
- hosts: group1
  roles:
    - role1
    - role2

- hosts: group2
  roles:
    - role2
    - role3

```

Now say I have a hosts file that has an entry like this:

```auto
[group1]
host1.example.com

[group2]
host1.example.com

```

Ansible will run the tasks in role2 TWICE for [host1.example.com](http://host1.example.com) because it appears in 2 groups, and each have role2 assigned to them.

How can I make Ansible realise it has the same role included twice, and thus it should only run it once?

Thanks

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 10, 2015, 2:04pm UTC](https://forum.ansible.com/t/a-host-appears-in-more-than-one-group-and-both-groups-have-the-same-tasks-in-them-any-way-to-run-tasks-once/17489/2 "2015-02-10T14:04:18Z")

</div>

the problem is that you are telling ansible to run the role x2 on that  
host, imagine you do this:

- hosts: group1  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- role1  
&nbsp;&nbsp;&nbsp;&nbsp;- role2

- hosts: group1  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- role2  
&nbsp;&nbsp;&nbsp;&nbsp;- role3

ansible would run the role2 on all hosts in group1 twice, it is just  
doing what you ask.

if role2 is idempotent this should not really matter much

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 10, 2015, 2:05pm UTC](https://forum.ansible.com/t/a-host-appears-in-more-than-one-group-and-both-groups-have-the-same-tasks-in-them-any-way-to-run-tasks-once/17489/3 "2015-02-10T14:05:41Z")

</div>

there are several ways to get around the issue if you really want, this is one:

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [February 10, 2015, 2:07pm UTC](https://forum.ansible.com/t/a-host-appears-in-more-than-one-group-and-both-groups-have-the-same-tasks-in-them-any-way-to-run-tasks-once/17489/4 "2015-02-10T14:07:03Z")

</div>

This is by design.  
The only way to go would be to apply role2 only in one playbook to one specific group, and not use role2 in any other playbook on a group that might have common members, like here.

---

<div class="post-metadata">

**Author:** ![Asfand\_Yar\_Qazi](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Asfand\_Yar\_Qazi](https://forum.ansible.com/u/Asfand_Yar_Qazi)\
**Post date:** [February 11, 2015, 10:32am UTC](https://forum.ansible.com/t/a-host-appears-in-more-than-one-group-and-both-groups-have-the-same-tasks-in-them-any-way-to-run-tasks-once/17489/5 "2015-02-11T10:32:52Z")

</div>

Thanks everyone, I went with making my roles more granular and making sure each host appeared in only one play.

Thanks
