Ben
Ben

Reputation: 1023

using ansible set_fact module to define persistent facts?

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

Upvotes: 15

Views: 12719

Answers (3)

bbaassssiiee
bbaassssiiee

Reputation: 6782

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

Upvotes: 2

senorsmile
senorsmile

Reputation: 760

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.

Upvotes: 2

Bruce P
Bruce P

Reputation: 20719

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.

Upvotes: 1

Related Questions