# storing Ansible facts in mysql database

**URL:** <https://forum.ansible.com/t/storing-ansible-facts-in-mysql-database/21329>\
**Category:** Ansible Project\
**Created:** [May 2, 2016, 11:47am UTC](https://forum.ansible.com/t/storing-ansible-facts-in-mysql-database/21329 "2016-05-02T11:47:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_Dasmala](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Anil\_Dasmala](https://forum.ansible.com/u/Anil_Dasmala)\
**Post date:** [May 2, 2016, 11:47am UTC](https://forum.ansible.com/t/storing-ansible-facts-in-mysql-database/21329/1 "2016-05-02T11:47:20Z")

</div>

Hi Guys,

Can someone guide me on how to store the facts for all hosts in inventory into a mysql database.  
In other words, output data of setup module to mysql.  
Appreciate ideas/suggestions.

–Anil

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 2, 2016, 12:46pm UTC](https://forum.ansible.com/t/storing-ansible-facts-in-mysql-database/21329/2 "2016-05-02T12:46:27Z")

</div>

A fact cache plugin can do this easily, check the memcached and redis ones as an example.

---

<div class="post-metadata">

**Author:** ![Uditha\_Desilva](https://avatars.discourse-cdn.com/v4/letter/u/f475e1/32.png) [@Uditha\_Desilva](https://forum.ansible.com/u/Uditha_Desilva)\
**Post date:** [May 3, 2016, 7:34am UTC](https://forum.ansible.com/t/storing-ansible-facts-in-mysql-database/21329/3 "2016-05-03T07:34:33Z")

</div>

The host facts are dictionary objects and will most likely not have exactly the same number of elements within them (unless you have a trivially uniform environment!) so a MySQL backend isn’t a particularly useful fit.

Personally (on Red Hat), I prefer to use the jsonfile backend and a 1 day validity, but specifying a directory under /dev/shm to store the files, thereby effectively running it in a ramdisk. This provides a decent performance gain while still allowing the standard Unix file system security to be applied.

---

<div class="post-metadata">

**Author:** ![Anil\_Dasmala](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Anil\_Dasmala](https://forum.ansible.com/u/Anil_Dasmala)\
**Post date:** [May 3, 2016, 9:57am UTC](https://forum.ansible.com/t/storing-ansible-facts-in-mysql-database/21329/4 "2016-05-03T09:57:37Z")

</div>

thanks for your advice,Brian. Tried redis, so each host (key)and its facts(value) are stored as a key value pair. is there a way I can get the selective facts with in a given key value?.  
For example, if I just want to retrieve testhost1’s HOSTNAME, ansible\_default\_ipv4 only, is that possible  
Essentially I wanted to parse the selective facts output into a excel/csv with one row per host in easy readable format.

---

<div class="post-metadata">

**Author:** ![Anil\_Dasmala](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Anil\_Dasmala](https://forum.ansible.com/u/Anil_Dasmala)\
**Post date:** [May 3, 2016, 9:58am UTC](https://forum.ansible.com/t/storing-ansible-facts-in-mysql-database/21329/5 "2016-05-03T09:58:26Z")

</div>

Thank you for the ideas, Uditha Desilva.
