Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using ansible set_fact module to define persistent facts?

Tags:

ansible

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

like image 592
Ben Avatar asked Mar 13 '15 00:03

Ben


2 Answers

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

  • Assign them to hosts using your language of choice via Ansible's Dynamic Inventory:

    • http://docs.ansible.com/ansible/intro_dynamic_inventory.html
    • http://docs.ansible.com/ansible/developing_inventory.html

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.
like image 69
senorsmile Avatar answered Sep 20 '22 01:09

senorsmile


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

like image 45
bbaassssiiee Avatar answered Sep 20 '22 01:09

bbaassssiiee