# test host for group membership

**URL:** <https://forum.ansible.com/t/test-host-for-group-membership/12022>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [October 24, 2012, 11:32am UTC](https://forum.ansible.com/t/test-host-for-group-membership/12022 "2012-10-24T11:32:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 24, 2012, 11:32am UTC](https://forum.ansible.com/t/test-host-for-group-membership/12022/1 "2012-10-24T11:32:43Z")

</div>

Here's one way (assuming 0.8 syntax). Just have two short plays  
that talk to each group, and use the group-exclusion features.

hosts: virt:!virt-managed  
tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- template: ...

hosts: virt-managed  
tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- template: ...

(Alternatively, create a group called virt-unmanaged, and make the  
group virt contain those other two groups as child groups)

You can then, in other steps before or after, address the virt group  
as a whole. Playbooks can contain as many plays as you want  
so it's fine to use different hosts selectors.

I think lots of people end up getting carried away with "only\_if" as a  
conditional check, and that's ok, but it's usually not neccessary.  
I'll leave it someone else to post the only\_if version 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 24, 2012, 12:14pm UTC](https://forum.ansible.com/t/test-host-for-group-membership/12022/2 "2012-10-24T12:14:46Z")

</div>

Another method -- perhaps nicer, perhaps not, is to set a default  
variable for the "virt" group using a "group\_vars/virt" file in a  
location relative to the playbook.

Set another variable in "group\_vars/virt\_managed".

The more specific group will override the other.

Let's say it was $filepath

Then just do src=$filepath/foo.conf

This allows for one playbook.
