# Patterns and precedence

**URL:** <https://forum.ansible.com/t/patterns-and-precedence/17494>\
**Category:** Ansible Project\
**Created:** [February 10, 2015, 8:47pm UTC](https://forum.ansible.com/t/patterns-and-precedence/17494 "2015-02-10T20:47:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [February 10, 2015, 8:47pm UTC](https://forum.ansible.com/t/patterns-and-precedence/17494/1 "2015-02-10T20:47:56Z")

</div>

[http://docs.ansible.com/intro\_patterns.html](http://docs.ansible.com/intro_patterns.html) shows

&nbsp;&nbsp;webservers:dbservers:&staging:!phoenix

as an example of a combination of unions and intersections, and says that  
this means

&nbsp;&nbsp;all machines in the groups `webservers' and `dbservers' are to be  
&nbsp;&nbsp;managed if they are in the group `staging' also, but the machines are  
&nbsp;&nbsp;not to be managed if they are in the group `phoenix'

To simplify slightly, because I don't actualy need the negation in my  
example, it's saying that

&nbsp;&nbsp;webservers:dbservers:&staging

means

&nbsp;&nbsp;(websevers UNION dbservers) INTERSECTION staging

which is cool.

But, I have a use case where I want to say

&nbsp;&nbsp;webservers UNION (dbservers INTERSECTION staging)

i.e. I want to manage with all webservers and only staging dbservers. I  
didn't see an obvious way to do that; all three of

&nbsp;&nbsp;webservers:dbservers:&staging  
&nbsp;&nbsp;webservers:staging:&dbservers  
&nbsp;&nbsp;dbservers:&staging:webservers  
&nbsp;&nbsp;staging:&dbservers:webservers

seem to be equivalent, and adding parentheses didn't seem to be supported  
(I got "ERROR: provided hosts list is empty").

I particularly want to use it in a --limit argument, if that matters.

Is there a way to do this, or does union always take precedence over  
intersection?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [February 13, 2015, 12:44pm UTC](https://forum.ansible.com/t/patterns-and-precedence/17494/2 "2015-02-13T12:44:59Z")

</div>

There magic happens in lib/ansible/inventory.get\_hosts() [1].

# when applying the host selectors, run those without the "&" or "!"  
# first, then the &s, then the !s.  
patterns = pattern\_regular + pattern\_intersection + pattern\_exclude

[...]

if p.startswith("!"):  
&nbsp;&nbsp;hosts = [h for h in hosts if h not in that]  
elif p.startswith("&"):  
&nbsp;&nbsp;hosts = [h for h in hosts if h in that]  
else:  
&nbsp;&nbsp;to\_append = [ h for h in that if h.name not in [ y.name for y in hosts  
&nbsp;&nbsp;] ]  
&nbsp;&nbsp;hosts.extend(to\_append)

My brain can't find a way to define what you want using patterns alone.

Filled issue#10234 as a possible improvement [2].

[1] -  
[https://github.com/ansible/ansible/blob/devel/lib/ansible/inventory/\_\_init\_\_.py#L176](https://github.com/ansible/ansible/blob/devel/lib/ansible/inventory/ __init__.py#L176)  
[2] - [https://github.com/ansible/ansible/issues/10234](https://github.com/ansible/ansible/issues/10234)

Giovanni
