# Storing facts centrally

**URL:** <https://forum.ansible.com/t/storing-facts-centrally/11880>\
**Category:** Ansible Project\
**Created:** [September 11, 2012, 9:17am UTC](https://forum.ansible.com/t/storing-facts-centrally/11880 "2012-09-11T09:17:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nigel\_Metheringham](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nigel\_Metheringham](https://forum.ansible.com/u/Nigel_Metheringham)\
**Post date:** [September 11, 2012, 9:17am UTC](https://forum.ansible.com/t/storing-facts-centrally/11880/1 "2012-09-11T09:17:14Z")

</div>

Is there a mechanism to save the facts from the setup module in a local  
file on the central machine?

ie a sort of collision between the fetch and setup modules 🙂

[I know I can run setup as a ansible standalone command, but I'd like to  
be able to save the setup from a whole set of machines from within a  
playbook]

&nbsp;&nbsp;Nigel.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [September 11, 2012, 9:40am UTC](https://forum.ansible.com/t/storing-facts-centrally/11880/2 "2012-09-11T09:40:18Z")

</div>

Nigel,

> Is there a mechanism to save the facts from the setup module in a local  
> file on the central machine?

Yes there is, using a callback. (I'm crafting an article on how to do  
that as we speak.) Have a look at lib/ansible/callback\_plugins/  
specifically the `log\_plays.py' example therein.

Ansible used to drop the setup on the managed nodes, but that feature  
was removed. Callbacks allow us to, say, take the facts and shove them  
into a database.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<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:** [September 11, 2012, 12:01pm UTC](https://forum.ansible.com/t/storing-facts-centrally/11880/3 "2012-09-11T12:01:20Z")

</div>

> Ansible used to drop the setup on the managed nodes, but that feature  
> was removed. Callbacks allow us to, say, take the facts and shove them  
> into a database.

Ansible-commander is going to contain one of these to allow browsing  
the current state of facts on all nodes, as well as finding nodes  
based on fact-values. However, to get something like fetch, it's simpler:

ansible all -m setup --tree /tmp/dump\_path

Which will save one JSON file per host name in the "dump\_path" directory.

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [September 14, 2012, 4:57pm UTC](https://forum.ansible.com/t/storing-facts-centrally/11880/4 "2012-09-14T16:57:19Z")

</div>

I'm just catching up on some of the other threads here.

I solved this with a template and a playbook. The playbook collects the facts -- nodeinfo is a module I developed that collects other facts that Ansible's default set does not handle.

I then run a task a template on the localhost to generate the file with the to\_json filter.

The playbook:

- name: collect and cache facts about hosts  
&nbsp;&nbsp;hosts: all:!localhost  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: collect node info  
&nbsp;&nbsp;&nbsp;&nbsp;action: nodeinfo  
- name: generate facts file  
&nbsp;&nbsp;hosts: localhost  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: writing facts  
&nbsp;&nbsp;&nbsp;&nbsp;action: template src=templates/hostvars.j2 dest=~/facts.json

The template:

{{ hostvars|to\_json|safe }}

If you don't have extra node info to collect like me, you have a couple of options. Do a ping in the first play -- simple quick and harmless. Another thing I could have done is combined the two plays and write a local file per host something like this (untested):

hosts: all!localhost  
tasks:  
&nbsp;&nbsp;- name: writing facts  
&nbsp;&nbsp;&nbsp;&nbsp;action: template src=templates/hostvars.j2 dest=~/${inventory\_hostname}.facts.json  
&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

{{ hostvars[inventory\_hostname] | to\_json | safe }}

The one thing I don't like so much about tjis solution is creating such a tiny never changing "template" to achieve this. Being able to pass a string instead of a filename to the template engine would be nice.

\<tim/\>
