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

80 views
Skip to first unread message

Asfand Yar Qazi

unread,
Feb 10, 2015, 8:47:41 AM2/10/15
to ansible...@googlegroups.com
Hi,

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


I have a playbook that looks something like this:

---
- hosts: group1
  roles:
    - role1
    - role2

- hosts: group2
  roles:
    - role2
    - role3

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

[group1]
host1.example.com

[group2]
host1.example.com

Ansible will run the tasks in role2 TWICE for 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

Brian Coca

unread,
Feb 10, 2015, 9:04:22 AM2/10/15
to ansible...@googlegroups.com
the problem is that you are telling ansible to run the role x2 on that
host, imagine you do this:

- hosts: group1
roles:
- role1
- role2

- hosts: group1
roles:
- role2
- 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

Brian Coca

unread,
Feb 10, 2015, 9:05:44 AM2/10/15
to ansible...@googlegroups.com
there are several ways to get around the issue if you really want, this is one:

---
- hosts: group1
roles:
- role1

- hosts: "group1:group2"
roles:
- role2

- hosts: group2
roles:
- role3



--
Brian Coca

Serge van Ginderachter

unread,
Feb 10, 2015, 9:07:27 AM2/10/15
to ansible...@googlegroups.com
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.

--
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/452eae39-22b7-4ff3-a100-dbe8854e533f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Asfand Yar Qazi

unread,
Feb 11, 2015, 5:32:52 AM2/11/15
to ansible...@googlegroups.com
Thanks everyone, I went with making my roles more granular and making sure each host appeared in only one play.

Thanks
Reply all
Reply to author
Forward
0 new messages