# Define group in hosts file based on intersection of 2 groups

**URL:** <https://forum.ansible.com/t/define-group-in-hosts-file-based-on-intersection-of-2-groups/22445>\
**Category:** Ansible Project\
**Created:** [September 1, 2016, 9:27am UTC](https://forum.ansible.com/t/define-group-in-hosts-file-based-on-intersection-of-2-groups/22445 "2016-09-01T09:27:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iain\_M\_Conochie](https://avatars.discourse-cdn.com/v4/letter/i/3ab097/32.png) [@Iain\_M\_Conochie](https://forum.ansible.com/u/Iain_M_Conochie)\
**Post date:** [September 1, 2016, 9:27am UTC](https://forum.ansible.com/t/define-group-in-hosts-file-based-on-intersection-of-2-groups/22445/1 "2016-09-01T09:27:24Z")

</div>

Hello,

It is possible to limit the hosts the ansible command runs against by specifying patterns on the command line like so:

ansible webservers:!staging -m ping

Is it possible to use the same technique to define groups in the inventory file, so that the new group is defined by the intersection of 2 child groups?

Thanks

Iain

---

<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:** [September 20, 2016, 3:20pm UTC](https://forum.ansible.com/t/define-group-in-hosts-file-based-on-intersection-of-2-groups/22445/2 "2016-09-20T15:20:52Z")

</div>

No, that is only available in the `hosts:` clause of a play, with --limit and for `ansible` in the host pattern specified (equivalent to `hosts:` in a play).

---

<div class="post-metadata">

**Author:** ![Raj3](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@Raj3](https://forum.ansible.com/u/Raj3)\
**Post date:** [August 15, 2017, 3:43pm UTC](https://forum.ansible.com/t/define-group-in-hosts-file-based-on-intersection-of-2-groups/22445/3 "2017-08-15T15:43:50Z")

</div>

That should be possible.  
I have been doing something like that using the escape character in command line. Ansible version 2.3  
ansible webserver:&dev -m ping  
ansible webserver:!prod -m ping

Thanks.
