Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to gather facts to fact cache

I am trying to make Ansible work with --limit and to do that I need facts about other hosts, which I am caching with fact_caching. What command should I run so that it simply gathers all the facts on all the hosts and caches them, without running any tasks? Something like the setup module would be perfect if it cached the facts it gathered, but it seems like it does not.

like image 517
Sepehr Nazari Avatar asked Sep 21 '15 20:09

Sepehr Nazari


People also ask

Does Ansible cache facts?

Fact cachingBy default, Ansible gathers facts for each host at the beginning of every play, unless gather_facts is set to false . With a large number of hosts this can result in a significant amount of time spent gathering facts. One way to improve this is through Ansible's support for fact caching.

How do I clear Ansible fact cache?

New facts will be gathered and cached during the next playbook run. To clear facts for a single host, find its file within /etc/openstack_deploy/ansible_facts/ and remove it. Each host has a JSON file that is named after its hostname. The facts for that host will be regenerated on the next playbook run.

What does gather facts do in Ansible?

Ansible facts are data gathered about target nodes (host nodes to be configured) and returned back to controller nodes. Ansible facts are stored in JSON format and are used to make important decisions about tasks based on their statistics.

Where does Ansible store cache?

Ansible provides Cache plugins which can store the gathered facts. If the system facts don't change between Playbook runs, this will greatly speed up the runtime of Playbooks. The facts cache can be stored in JSON files, in a Redis DB, in a Memcache, and a few other options.


1 Answers

Here is how I'd solve the problem:

1.- Enable facts gathering on your playbook (site.yml):

gather_facts: yes

2.- Enable facts caching on ansible.cfg:

2.1.- Option 1 - Use this if you have the time to install redis:

[defaults]
gathering = smart
fact_caching = redis

# two hours timeout
fact_caching_timeout = 7200

2.2.- Option 2 - Use this to test right now is simple but slower than redis:

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/facts_cache

# two hours timeout
fact_caching_timeout = 7200

3.- Update or create the facts cache. To do this create a new role (cache-update) with just one task: execute ping. We use ping because is the simplest and fastest ansible task so it will help us update the cache really fast:

- name: Pinging server to update facts cache
  ping:             

Greetings,

like image 115
alfredocambera Avatar answered Sep 23 '22 12:09

alfredocambera