# How to save whole ansible\_facts to file

**URL:** <https://forum.ansible.com/t/how-to-save-whole-ansible-facts-to-file/26771>\
**Category:** Ansible Project\
**Created:** [February 17, 2018, 8:38am UTC](https://forum.ansible.com/t/how-to-save-whole-ansible-facts-to-file/26771 "2018-02-17T08:38:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![YvanM](https://avatars.discourse-cdn.com/v4/letter/y/65b543/32.png) [@YvanM](https://forum.ansible.com/u/YvanM)\
**Post date:** [February 17, 2018, 8:38am UTC](https://forum.ansible.com/t/how-to-save-whole-ansible-facts-to-file/26771/1 "2018-02-17T08:38:24Z")

</div>

Hi,

Running Ansible 2.4, I am trying to save **all** Ansible facts to a file, with a task like this:

`

- name: save all facts to host specific file  
copy:  
content: “{{ ansible\_facts }}”  
dest: “/some/directory/{{ ansible\_fqdn }}”  
delegate\_to: localhost  
`

However it fails with the error “‘ansible\_facts’ is undefined”.

Do you think there is a workaround? I really do not want to have to select facts one by one…

Regards,  
Yvan

---

<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:** [February 17, 2018, 9:19am UTC](https://forum.ansible.com/t/how-to-save-whole-ansible-facts-to-file/26771/2 "2018-02-17T09:19:58Z")

</div>

The facts is not stored under "ansible\_facts" but "vars" so use

&nbsp;&nbsp;content: '{{ vars }}'

---

<div class="post-metadata">

**Author:** ![YvanM](https://avatars.discourse-cdn.com/v4/letter/y/65b543/32.png) [@YvanM](https://forum.ansible.com/u/YvanM)\
**Post date:** [February 17, 2018, 10:34am UTC](https://forum.ansible.com/t/how-to-save-whole-ansible-facts-to-file/26771/3 "2018-02-17T10:34:19Z")

</div>

Thanks for the hint!

I forgot to say that I want to save all Ansible facts **host by host** , so “{{ vars }}” contains to much data for me. Here is what I found to remove vars from master and other hosts:

`

- name: save all facts to host specific file  
copy:  
content: “{{ ansible\_delegated\_vars[inventory\_hostname].vars | to\_nice\_json }}”  
`dest: "/some/directory/{{ ansible_fqdn }}"`  
delegate\_to: localhost  
`

This still contains variables from inventory, but it is better than nothing…

---

<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:** [February 17, 2018, 11:00am UTC](https://forum.ansible.com/t/how-to-save-whole-ansible-facts-to-file/26771/4 "2018-02-17T11:00:19Z")

</div>

> Thanks for the hint!
> 
> I forgot to say that I want to save all Ansible facts \*host by host\*, so  
> "{{ vars }}" contains to much data for me. Here is what I found to remove  
> vars from master and other hosts:

Look into the facts caching feature of Ansible, that will only store the gathered facts and not inventory in one file per host.  
[https://docs.ansible.com/ansible/latest/playbooks\_variables.html#fact-caching](https://docs.ansible.com/ansible/latest/playbooks_variables.html#fact-caching)

> &nbsp;&nbsp;- name: save all facts to host specific file  
> &nbsp;&nbsp;&nbsp;&nbsp;copy:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ ansible\_delegated\_vars[inventory\_hostname].vars |  
> to\_nice\_json }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "/some/directory/{{ ansible\_fqdn }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost
> 
> This still contains variables from inventory, but it is better than nothing…

This will also only store the gathered facts and not inventory

&nbsp;&nbsp;- setup:  
&nbsp;&nbsp;&nbsp;&nbsp;register: setupvar

&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: '{{ setupvar.ansible\_facts }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /some/directory/{{ ansible\_fqdn }}  
&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

---

<div class="post-metadata">

**Author:** ![YvanM](https://avatars.discourse-cdn.com/v4/letter/y/65b543/32.png) [@YvanM](https://forum.ansible.com/u/YvanM)\
**Post date:** [February 17, 2018, 11:17am UTC](https://forum.ansible.com/t/how-to-save-whole-ansible-facts-to-file/26771/5 "2018-02-17T11:17:32Z")

</div>

I did not think about registering results of “setup” task, that is perfect!

Thanks,  
Yvan

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 19, 2018, 4:13pm UTC](https://forum.ansible.com/t/how-to-save-whole-ansible-facts-to-file/26771/6 "2018-02-19T16:13:21Z")

</div>

Use the jsonfile ansible cache, it will automatically store facts (and  
only facts) into files.

In 2.5 we do have facts under ansible\_facts and we hope to ONLY have  
them there in the future, currently it is a toggle to allow them in  
'main vars' or not.
