# Using ansible facts with when or only\_if

**URL:** <https://forum.ansible.com/t/using-ansible-facts-with-when-or-only-if/12166>\
**Category:** Ansible Project\
**Created:** [December 27, 2012, 1:03am UTC](https://forum.ansible.com/t/using-ansible-facts-with-when-or-only-if/12166 "2012-12-27T01:03:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dylan\_Silva](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@Dylan\_Silva](https://forum.ansible.com/u/Dylan_Silva)\
**Post date:** [December 27, 2012, 1:03am UTC](https://forum.ansible.com/t/using-ansible-facts-with-when-or-only-if/12166/1 "2012-12-27T01:03:33Z")

</div>

Hello all!

I am trying to use ansible fact names (mainly group\_names) with both when\_string and only\_if, but am hitting a road block. It seems to me that if I run a playbook using a child group like foo:children but in my play book I call out when\_string or only\_if for bar1 and bar2 groups (children of foo:children) the plays do not get run against them. Is this not an intended method for usage of when and only\_if?

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [December 28, 2012, 3:14pm UTC](https://forum.ansible.com/t/using-ansible-facts-with-when-or-only-if/12166/2 "2012-12-28T15:14:12Z")

</div>

Dylan Silva wrote:

> Hello all!
> 
> I am trying to use ansible fact names (mainly group\_names) with both  
> when\_string and only\_if, but am hitting a road block. It seems to me that  
> if I run a playbook using a child group like foo:children but in my play  
> book I call out when\_string or only\_if for bar1 and bar2 groups (children  
> of foo:children) the plays do not get run against them. Is this not an  
> intended method for usage of when and only\_if?

group\_names should work fine, we'd have to see your playbook and inventory to  
really be able to say much more. Note though that if you are targeting  
different hosts, you really should have multiple plays doing so, that is what  
hosts: is for.

Daniel

---

<div class="post-metadata">

**Author:** ![Dylan\_Silva](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@Dylan\_Silva](https://forum.ansible.com/u/Dylan_Silva)\
**Post date:** [December 29, 2012, 12:47am UTC](https://forum.ansible.com/t/using-ansible-facts-with-when-or-only-if/12166/3 "2012-12-29T00:47:32Z")

</div>

Thanks for the clarification. Unfortunately, something is going wrong with my playbook then, and I think I know the problem.

So the command I am running is

ansible-playbook /opt/ansible-playbooks/application/management/setup.yml --extra-vars=“hosts=application1” --verbose

My playbook has the following in it:

---

<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:** [December 29, 2012, 12:53am UTC](https://forum.ansible.com/t/using-ansible-facts-with-when-or-only-if/12166/4 "2012-12-29T00:53:16Z")

</div>

No, using a group includes child groups, I don’t think you can pass hosts that way.

Try this in playbook

- hosts: $target

Then use -e ‘target=application1’

Brian Coca

---

<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:** [December 29, 2012, 1:11am UTC](https://forum.ansible.com/t/using-ansible-facts-with-when-or-only-if/12166/5 "2012-12-29T01:11:25Z")

</div>

For one, it's "only\_if: 'appApp' in $group\_names" if you \*did\* want to  
do that, because group\_names is an array, and not a string.

But, really, this is not the way ansible likes to do things.

It really loves this (as Daniel said):

hosts: appApp  
tasks:  
&nbsp;&nbsp;&nbsp;- ...

hosts: appDb  
tasks:  
&nbsp;&nbsp;&nbsp;- ...

Groups are already a hosts slicing mechanism (it's what they were made  
for), so it makes it much simpler that way. only\_if only exists for  
the weird corner cases, if you find it in your playbooks, you should  
really ask if it needs to be there.

If you do have to slice hosts up by arbitrary discovered criteria  
(like OS type), the group\_by module is also excellent for this.

--Michael

---

<div class="post-metadata">

**Author:** ![Dylan\_Silva](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@Dylan\_Silva](https://forum.ansible.com/u/Dylan_Silva)\
**Post date:** [December 29, 2012, 1:17am UTC](https://forum.ansible.com/t/using-ansible-facts-with-when-or-only-if/12166/6 "2012-12-29T01:17:57Z")

</div>

Great, this makes sense to me now! Thanks for the awesome suggestions!
