# Complex host patterns

**URL:** <https://forum.ansible.com/t/complex-host-patterns/28849>\
**Category:** Ansible Project\
**Created:** [October 23, 2018, 6:24am UTC](https://forum.ansible.com/t/complex-host-patterns/28849 "2018-10-23T06:24:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [October 23, 2018, 6:24am UTC](https://forum.ansible.com/t/complex-host-patterns/28849/1 "2018-10-23T06:24:37Z")

</div>

Hi All

When working with [host patterns](https://docs.ansible.com/ansible/2.7/user_guide/intro_patterns.html), is it possible to craft one that says “must be in group X _and_ must be in either group Y, W and Z”?

e.g. host must be in group “webserver” but it _must_ also be in either group dc1, dc2 or dc3.

I can see how to check that webserver is part of dc1 (webserver,&dc1) but not sure how to build it with multiple “OR” on the “AND” (something like “webserver,&(dc1,dc2,dc3)” is what I’m looking for but it doesn’t work)

Cheers  
Gonz

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 23, 2018, 8:41am UTC](https://forum.ansible.com/t/complex-host-patterns/28849/2 "2018-10-23T08:41:35Z")

</div>

There is an example in the doc (you posted the link).

webservers:dbservers:&staging  
The above configuration means “all machines in the groups ‘webservers’ and ‘dbservers’ are to be managed if they are in the group ‘staging’ also, …

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [October 23, 2018, 10:30am UTC](https://forum.ansible.com/t/complex-host-patterns/28849/3 "2018-10-23T10:30:37Z")

</div>

Not quite. How about “all machines in the groups webservers and dbservers are to be managed only if they are part of staging OR qa”?

---

<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:** [October 23, 2018, 2:13pm UTC](https://forum.ansible.com/t/complex-host-patterns/28849/4 "2018-10-23T14:13:39Z")

</div>

Put the dc1, dc2 and dc3 group in a group called dc and then you can use

&nbsp;&nbsp;webserver:&dc

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [October 23, 2018, 3:29pm UTC](https://forum.ansible.com/t/complex-host-patterns/28849/5 "2018-10-23T15:29:35Z")

</div>

You’re trying to do it backwards. Build the union of your other groups and do the intersection with webserver.

&webserver,dc1,dc2,dc3

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [October 24, 2018, 12:24am UTC](https://forum.ansible.com/t/complex-host-patterns/28849/6 "2018-10-24T00:24:44Z")

</div>

Thanks, good suggestion. I suppose there’s no way to say “must be webserver OR dbserver AND in (dc1 OR dc2 OR dc3)”?

- Gonz

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [October 24, 2018, 12:26am UTC](https://forum.ansible.com/t/complex-host-patterns/28849/7 "2018-10-24T00:26:16Z")

</div>

I’d like to be able to do this on the fly so creating a custom group like that is not ideal.

- Gonz

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [October 24, 2018, 1:17am UTC](https://forum.ansible.com/t/complex-host-patterns/28849/8 "2018-10-24T01:17:39Z")

</div>

Not in a host pattern. no. If you can’t do it in the inventory, you can do it with a dynamic group:

`

- hosts: localhost  
gather\_facts: false  
tasks:

- add\_host:  
name: “{{ item }}”  
groups: target\_hosts  
when: hostvars[item].group\_names | intersect([‘dc1’, ‘dc2’, ‘dc3’])  
loop: “{{ groups.webserver | union(groups.dbserver) }}”

- hosts: target\_hosts  
tasks:

- command: /bin/true

`

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [October 24, 2018, 1:27am UTC](https://forum.ansible.com/t/complex-host-patterns/28849/9 "2018-10-24T01:27:58Z")

</div>

Thanks for that! Very useful.

- Gonz
