# Host filter / patterns

**URL:** <https://forum.ansible.com/t/host-filter-patterns/19080>\
**Category:** Ansible Project\
**Created:** [August 21, 2015, 10:09am UTC](https://forum.ansible.com/t/host-filter-patterns/19080 "2015-08-21T10:09:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bouke](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Bouke](https://forum.ansible.com/u/Bouke)\
**Post date:** [August 21, 2015, 10:09am UTC](https://forum.ansible.com/t/host-filter-patterns/19080/1 "2015-08-21T10:09:01Z")

</div>

For the life of me I can’t figure out how to match the right hosts on my playbook like this:

> app AND (production OR staging)

I tried “app:&production:staging” but ansible turns that into “(app AND production) OR staging”.  
Thinking that AND “:&” binds before OR “:”, I tried this: “app:&production:app:&staging”, which doesn’t match any thing at all.

Ansible has no grouping? So how to solve this trivial issue?

---

<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:** [August 21, 2015, 3:12pm UTC](https://forum.ansible.com/t/host-filter-patterns/19080/2 "2015-08-21T15:12:21Z")

</div>

The conditions are not parsed in order (its on my todo list) but  
globally, so the position of &production does not currently matter and  
it gets applied after all the 'ANDs'.

---

<div class="post-metadata">

**Author:** ![Bouke](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Bouke](https://forum.ansible.com/u/Bouke)\
**Post date:** [September 4, 2015, 4:35pm UTC](https://forum.ansible.com/t/host-filter-patterns/19080/3 "2015-09-04T16:35:46Z")

</div>

i see… so the only way to solve it is by creating a new group like “stage\_or\_production” and then filter by “app:&stage\_or\_production”?

my suggestion for implementing logical groups:

hosts:

- app:&stage
- app:&production

meaning (app AND stage) OR (app AND production)

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [September 4, 2015, 9:30pm UTC](https://forum.ansible.com/t/host-filter-patterns/19080/4 "2015-09-04T21:30:08Z")

</div>

Maybe I overlooked something…  
But wouldn’t that be the same as “staging:production:&app”?

esco
