# How to select hosts based on facts?

**URL:** <https://forum.ansible.com/t/how-to-select-hosts-based-on-facts/25174>\
**Category:** Ansible Project\
**Created:** [August 3, 2017, 4:01pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-facts/25174 "2017-08-03T16:01:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cev\_Ing](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Cev\_Ing](https://forum.ansible.com/u/Cev_Ing)\
**Post date:** [August 3, 2017, 4:01pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-facts/25174/1 "2017-08-03T16:01:08Z")

</div>

I wrote a role for Solaris systems. And now I would like to apply the role to all systems, which have been identified by Ansible to be a Solaris host.

I know that I can write an inventory group containing all Solaris servers. But this sounds redundant to me. Instead I want Ansible to check if the host is a Solaris host and if so, apply the role for Solaris systems.

How can this be done?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 3, 2017, 4:42pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-facts/25174/2 "2017-08-03T16:42:51Z")

</div>

With when  
[https://docs.ansible.com/ansible/latest/playbooks\_conditionals.html#the-when-statement](https://docs.ansible.com/ansible/latest/playbooks_conditionals.html#the-when-statement)

---

<div class="post-metadata">

**Author:** ![Michael\_Stroder](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Michael\_Stroder](https://forum.ansible.com/u/Michael_Stroder)\
**Post date:** [August 3, 2017, 4:58pm UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-facts/25174/3 "2017-08-03T16:58:21Z")

</div>

Cev Ing wrote:

> I know that I can write an inventory group containing all Solaris servers. But this  
> sounds redundant to me. Instead I want Ansible to check if the host is a Solaris host  
> and if so, apply the role for Solaris systems.

Maybe this helps:

[https://docs.ansible.com/ansible/latest/group\_by\_module.html](https://docs.ansible.com/ansible/latest/group_by_module.html)

Ciao, Michael.

---

<div class="post-metadata">

**Author:** ![Cev\_Ing](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Cev\_Ing](https://forum.ansible.com/u/Cev_Ing)\
**Post date:** [August 4, 2017, 5:13am UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-facts/25174/4 "2017-08-04T05:13:09Z")

</div>

I dont think it is possible to use a when statement on a role. Are you sure?

---

<div class="post-metadata">

**Author:** ![Cev\_Ing](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Cev\_Ing](https://forum.ansible.com/u/Cev_Ing)\
**Post date:** [August 4, 2017, 5:16am UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-facts/25174/5 "2017-08-04T05:16:33Z")

</div>

That seems to fit. Thanks!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 4, 2017, 8:25am UTC](https://forum.ansible.com/t/how-to-select-hosts-based-on-facts/25174/6 "2017-08-04T08:25:42Z")

</div>

All the allowed directives for a role is listed here  
[https://docs.ansible.com/ansible/latest/playbooks\_directives.html#role](https://docs.ansible.com/ansible/latest/playbooks_directives.html#role)  
and when is one of them.
