# Filter hosts based on facts

**URL:** <https://forum.ansible.com/t/filter-hosts-based-on-facts/17205>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [January 7, 2015, 11:43am UTC](https://forum.ansible.com/t/filter-hosts-based-on-facts/17205 "2015-01-07T11:43:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vaclav\_Adamec](https://avatars.discourse-cdn.com/v4/letter/v/a6a055/32.png) [@Vaclav\_Adamec](https://forum.ansible.com/u/Vaclav_Adamec)\
**Post date:** [January 7, 2015, 11:43am UTC](https://forum.ansible.com/t/filter-hosts-based-on-facts/17205/1 "2015-01-07T11:43:33Z")

</div>

Hi,  
can humbly ask someone to provide me some example how to filter list of servers with facts ? I probably miss something in documentation, I need something like this:

ansible -i /usr/local/src/ansible/plugins/inventory/vmware.py ‘~web.\*’ -m shell -a ‘filter=facter\_operatingsystem\_CentOS’ -a “some fancy command”

and in playbook this:

- name: Test  
script: ./script.sh  
register: command\_result  
failed\_when: “‘FAILED’ in command\_result.stdout”  
when: facter\_operatingsystem|CentOS

Thanks a lot

Vasek

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 7, 2015, 11:53am UTC](https://forum.ansible.com/t/filter-hosts-based-on-facts/17205/2 "2015-01-07T11:53:37Z")

</div>

Hi Vasek

You can declare dynamic groups based on arbitrary facts, and then apply tasks or roles to those groups. There is an example at [http://docs.ansible.com/playbooks\_best\_practices.html#operating-system-and-distribution-variance](http://docs.ansible.com/playbooks_best_practices.html#operating-system-and-distribution-variance) which seems to do exactly what you want.

Regards  
Tom

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 7, 2015, 11:55am UTC](https://forum.ansible.com/t/filter-hosts-based-on-facts/17205/3 "2015-01-07T11:55:04Z")

</div>

Of course, this approach applies to playbooks. I’m not sure how to (best) accomplish this with the standalone ansible command.

Regards  
Tom
