How to save whole ansible_facts to file

5,812 views
Skip to first unread message

YvanM

unread,
Feb 17, 2018, 3:38:24 AM2/17/18
to Ansible Project
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

Kai Stian Olstad

unread,
Feb 17, 2018, 4:20:14 AM2/17/18
to ansible...@googlegroups.com
On Saturday, 17 February 2018 09.38.24 CET YvanM wrote:
> 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…

The facts is not stored under "ansible_facts" but "vars" so use

content: '{{ vars }}'


--
Kai Stian Olstad

YvanM

unread,
Feb 17, 2018, 5:34:19 AM2/17/18
to Ansible Project
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…

Kai Stian Olstad

unread,
Feb 17, 2018, 6:00:46 AM2/17/18
to ansible...@googlegroups.com
On Saturday, 17 February 2018 11.34.19 CET YvanM wrote:
> 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


> - 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…

This will also only store the gathered facts and not inventory

- setup:
register: setupvar

- copy:
content: '{{ setupvar.ansible_facts }}'
dest: /some/directory/{{ ansible_fqdn }}
delegate_to: localhost


--
Kai Stian Olstad

YvanM

unread,
Feb 17, 2018, 6:17:32 AM2/17/18
to Ansible Project
I did not think about registering results of "setup" task, that is perfect!

Thanks,
Yvan

Brian Coca

unread,
Feb 19, 2018, 11:13:42 AM2/19/18
to Ansible Project
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.



--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages