# Apply playbooks based on system facts

**URL:** <https://forum.ansible.com/t/apply-playbooks-based-on-system-facts/12470>\
**Category:** Ansible Project\
**Created:** [March 8, 2013, 12:22pm UTC](https://forum.ansible.com/t/apply-playbooks-based-on-system-facts/12470 "2013-03-08T12:22:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin\_fernandes](https://avatars.discourse-cdn.com/v4/letter/k/db5fbb/32.png) [@kevin\_fernandes](https://forum.ansible.com/u/kevin_fernandes)\
**Post date:** [March 8, 2013, 12:22pm UTC](https://forum.ansible.com/t/apply-playbooks-based-on-system-facts/12470/1 "2013-03-08T12:22:50Z")

</div>

Hi All,

I have a current setup in puppet which applies the classes based on facter values on nodes. During provisioning phase i have a file called /etc/company\_facts.txt and has values like

role = dev\_tst  
platform = production  
location = london

On the puppetmaster side i assign/define the roles and classes based on facter facts rather than having to define node definitions.  
The above is a pull based setup and with mcollective i could do adhoc system management based on facts.

As i see that custom facts can be setup on the client side during provisioning phase easily. something like

custom\_role=dev\_tst  
custom\_platform=production  
custom\_location=london

My question is

- How can i eliminate all the host definitions in the inventory file in a push mode ansible or in ansible-pull setup, which will allow me to apply playbooks based on facts rather then node definitons in the inventory file.
- Allow me do adhoc system management based on facts rather than specifying node definitions in inventory file.

Whole purpose is to eliminate host definitions in the inventory file. Any suggestions/feedback would be helpful.

Regards,  
Kevin

---

<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:** [March 8, 2013, 3:10pm UTC](https://forum.ansible.com/t/apply-playbooks-based-on-system-facts/12470/2 "2013-03-08T15:10:07Z")

</div>

> Hi All,
> 
> I have a current setup in puppet which applies the classes based on facter  
> values on nodes. During provisioning phase i have a file called  
> /etc/company\_facts.txt and has values like

You can totally do if statements based on Ansible facts (or even reuse  
facts from facter or ohai, since the setup module will run them for  
you).

You don't even need if statements!

If you want to assign things that feel like roles though, the  
"group\_by" module is a great thing for this.

[http://ansible.cc/docs/modules.html#group-by](http://ansible.cc/docs/modules.html#group-by)

Here is an example:

[http://ansible.cc/docs/bestpractices.html#operating-system-and-distribution-variance](http://ansible.cc/docs/bestpractices.html#operating-system-and-distribution-variance)

> Whole purpose is to eliminate host definitions in the inventory file. Any  
> suggestions/feedback would be helpful.

This will eliminate your class definitions. If you don't want to keep  
up with inventory entirely, you can use an external inventory script  
to  
go further and pull inventory/group information from sources like EC2,  
Nova, or Cobbler -- or build your own. New in Ansible 1.1 (devel),  
Ansible can load a directory if "-i" specifies a directory and use  
multiple simultaneous inventory sources, including a mixture of files  
and dynamic sources.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![kevin\_fernandes](https://avatars.discourse-cdn.com/v4/letter/k/db5fbb/32.png) [@kevin\_fernandes](https://forum.ansible.com/u/kevin_fernandes)\
**Post date:** [March 9, 2013, 9:38am UTC](https://forum.ansible.com/t/apply-playbooks-based-on-system-facts/12470/3 "2013-03-09T09:38:54Z")

</div>

Hi Micheal,

The group\_by sounds a good alternative.

However how would running ad-hoc commands based on system facts work out with ansible?

I just had a wild solution for solving the above problem tonight. The client machines would register its facts in sort of CMDB on the ansible master machine and the ansible command line would read the CMDB and could extract the needed hosts from the requested facts that were presented on the command line. Just any idea. Let me know your feedback.

Regards,  
Kevin

---

<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 9, 2013, 3:16pm UTC](https://forum.ansible.com/t/apply-playbooks-based-on-system-facts/12470/4 "2013-03-09T15:16:19Z")

</div>

group\_by does not work with /usr/bin/ansible.

It's very likely when ansible-commander lifts off (starting next week)  
we can have iTunes-like smart groups just like you say in the near  
future, and you may not even have to use group\_by for some things.

TBD... but yes, an external inventory source can totally do this.

--Michael
