# check if host is part of group

**URL:** <https://forum.ansible.com/t/check-if-host-is-part-of-group/14017>\
**Category:** Ansible Project\
**Created:** [November 27, 2013, 7:24pm UTC](https://forum.ansible.com/t/check-if-host-is-part-of-group/14017 "2013-11-27T19:24:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khuong\_Dinh\_Pham](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khuong\_Dinh\_Pham](https://forum.ansible.com/u/Khuong_Dinh_Pham)\
**Post date:** [November 27, 2013, 7:24pm UTC](https://forum.ansible.com/t/check-if-host-is-part-of-group/14017/1 "2013-11-27T19:24:51Z")

</div>

Hi

I have this setup in my hosts

server1 ansible\_ssh\_host=[server1.xxx.com](http://server1.xxx.com)  
server2 ansible\_ssh\_host=[server2.yyy.com](http://server2.yyy.com)

server3 ansible\_ssh\_host=[server3.yyy.com](http://server3.yyy.com)

[frontendservers]  
server1  
server2  
server3

[frontendcluster1]

server1  
server2

My playbook:

---

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [November 27, 2013, 7:43pm UTC](https://forum.ansible.com/t/check-if-host-is-part-of-group/14017/2 "2013-11-27T19:43:49Z")

</div>

Hi Khuong,

Just create a group for these hosts, and change your hosts: line in the  
playbook :

[onlythose]  
server1  
server2

- hosts: onlythose  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: testing  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: copy some file

Cheers,

M

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 27, 2013, 7:44pm UTC](https://forum.ansible.com/t/check-if-host-is-part-of-group/14017/3 "2013-11-27T19:44:11Z")

</div>

Super easy!

- hosts: frontendcluster1  
tasks:  
…

You can have multiple plays in a playbook file.

For more complex things you can also say stuff like:

- hosts: group1:&group2

Which means “must be in both”

See [http://ansibleworks.com/docs/intro\_patterns.html](http://ansibleworks.com/docs/intro_patterns.html)

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [November 27, 2013, 7:46pm UTC](https://forum.ansible.com/t/check-if-host-is-part-of-group/14017/4 "2013-11-27T19:46:31Z")

</div>

almost always better to do as mentioned above but if you REAAAAAAALLLLYYYY need it:

when: inventory\_hostname in groups[‘frontendcluster1’]

---

<div class="post-metadata">

**Author:** ![Khuong\_Dinh\_Pham](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khuong\_Dinh\_Pham](https://forum.ansible.com/u/Khuong_Dinh_Pham)\
**Post date:** [November 27, 2013, 8:00pm UTC](https://forum.ansible.com/t/check-if-host-is-part-of-group/14017/5 "2013-11-27T20:00:18Z")

</div>

Hi

Thx. Just what I need.

It’s because I have one playbook for my frontendservers hosts. This playbook include roles but only some of the tasks should be deploy on a subset of the frontendservers. i.e. part of frontendcluster01 group

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 27, 2013, 8:02pm UTC](https://forum.ansible.com/t/check-if-host-is-part-of-group/14017/6 "2013-11-27T20:02:48Z")

</div>

when: inventory\_hostname in groups[‘frontendcluster1’]

Also more easily written as:

when: inventory\_hostname in groups.frontendcluster1

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [November 27, 2013, 10:17pm UTC](https://forum.ansible.com/t/check-if-host-is-part-of-group/14017/7 "2013-11-27T22:17:53Z")

</div>

Awesome tip! Much better than the quotation hell that I've been using 🙂

when "'frontendcluster1' in group\_names"

K

Kahlil (Kal) Hodgson GPG: C9A02289  
Head of Technology (m) +61 (0) 4 2573 0382  
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415  
401 Docklands Drive  
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that  
the parts you are reassembling were disassembled by you. Therefore,  
if you can't get them together again, there must be a reason. By all  
means, do not use a hammer." -- IBM maintenance manual, 1925
