# Specific playbook actions only when host is in other hostgroup

**URL:** <https://forum.ansible.com/t/specific-playbook-actions-only-when-host-is-in-other-hostgroup/12565>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [March 23, 2013, 3:37pm UTC](https://forum.ansible.com/t/specific-playbook-actions-only-when-host-is-in-other-hostgroup/12565 "2013-03-23T15:37:15Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [March 23, 2013, 3:37pm UTC](https://forum.ansible.com/t/specific-playbook-actions-only-when-host-is-in-other-hostgroup/12565/1 "2013-03-23T15:37:15Z")

</div>

only\_if: "'$inventory\_hostname' in ${groups.groupname}" seems to be  
what you want.

---

<div class="post-metadata">

**Author:** ![TextEditor](https://avatars.discourse-cdn.com/v4/letter/t/a88e57/32.png) [@TextEditor](https://forum.ansible.com/u/TextEditor)\
**Post date:** [March 23, 2013, 6:53pm UTC](https://forum.ansible.com/t/specific-playbook-actions-only-when-host-is-in-other-hostgroup/12565/2 "2013-03-23T18:53:08Z")

</div>

Deep down in the mailinglist I found:

only\_if: “‘groupname’ in $group\_names”

And the response below from Serge also states that. Why is your different, except for there being multiple ways to Rome? Which is (more) correct, and why?

---

<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:** [March 23, 2013, 9:14pm UTC](https://forum.ansible.com/t/specific-playbook-actions-only-when-host-is-in-other-hostgroup/12565/3 "2013-03-23T21:14:17Z")

</div>

They are just inverted ways of saying the same thing.

---

<div class="post-metadata">

**Author:** ![TextEditor](https://avatars.discourse-cdn.com/v4/letter/t/a88e57/32.png) [@TextEditor](https://forum.ansible.com/u/TextEditor)\
**Post date:** [March 24, 2013, 12:53pm UTC](https://forum.ansible.com/t/specific-playbook-actions-only-when-host-is-in-other-hostgroup/12565/4 "2013-03-24T12:53:22Z")

</div>

Ok!

---

<div class="post-metadata">

**Author:** ![TextEditor](https://avatars.discourse-cdn.com/v4/letter/t/a88e57/32.png) [@TextEditor](https://forum.ansible.com/u/TextEditor)\
**Post date:** [March 24, 2013, 5:19pm UTC](https://forum.ansible.com/t/specific-playbook-actions-only-when-host-is-in-other-hostgroup/12565/5 "2013-03-24T17:19:04Z")

</div>

Is it possible to get the only\_if statement working with a “- include playbook.yml” directive?

---

<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:** [March 24, 2013, 7:10pm UTC](https://forum.ansible.com/t/specific-playbook-actions-only-when-host-is-in-other-hostgroup/12565/6 "2013-03-24T19:10:58Z")

</div>

- include: foo  
&nbsp;&nbsp;only\_if: blarg

will attach the blarg on task includes.

It doesn't make any sense for playbook includes, as conditionals are  
not applied to whole playbooks, but to tasks.
