15
votes

I want to define a playbook which establishes facts about my hosts that can be used in other plays. The set_fact module claims to be able to do this ... http://docs.ansible.com/set_fact_module.html -- however it's not working ... The facts I define are available after the call to set_fact within a run of the play-book -- I would then expect to be able to use ansible all -m setup and see the fact defined somewhere within the facts gathered for each host ...

I tried looking into the code for the set_fact module -- but all I find is documentation string ... https://github.com/ansible/ansible-modules-core/blob/19b328c4df2157b6c0191e9144236643ce2be890/utilities/logic/set_fact.py

3
Good question and I did not know set_fact should work persistent. I tried to find the actual code, only found a reference that set_fact is an action plugin but was unable find the action plugins now that everything is split up in 3 repos. I found this thread from the mailing list which mentions an undocumented persistent parameter. Maybe play with that.udondan

3 Answers

2
votes

Firstly, the set_fact module only sets facts available during a run. For persistent facts, you'll need to either:

--Static--

  • define them in one of the following:
    • vars/
    • group_vars/
    • host_vars/

--Dynamic--

The latter is what I usually choose to do, as it is quite simple to set up, and the facts are always available on all hosts, even if you are doing something like:

  • getting all the facts for all the hosts while connected to a nagios host in order to generate its configuration files.
1
votes

What version of Ansible are you using? As of version 1.8, there is a built-in fact caching capability, but it's disabled by default. You would need to enable it within your ansible.cfg file, and you also need to have a redis server running since that's what actually caches the facts.

1
votes

If a remotely managed system has an /etc/ansible/facts.d directory, any files in this directory ending in .fact, can be JSON, INI, or executable files returning JSON, and these can supply local facts in Ansible, since 1.3. An alternate directory can be specified using the fact_path play directive.

http://docs.ansible.com/ansible/playbooks_variables.html#local-facts-facts-d