Sepehr Nazari
Sepehr Nazari

Reputation: 3875

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.

Upvotes: 21

Views: 28457

Answers (4)

activedecay
activedecay

Reputation: 10877

The following is an example showing how to use /usr/bin/ansible to run an ad-hoc task which gathers facts and stores them in the directory specified.

$ ANSIBLE_CONFIG=facts.cfg ansible r4i1 -i lab -m setup

Here's the contents of facts.cfg

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /home/lab/facts

And an example host's output

$ head /home/lab/facts/r4i1 
{
    "ansible_all_ipv4_addresses": [
        "10.10.20.89"
    ], 
    "ansible_all_ipv6_addresses": [
        "fe80::46a8:42ff:fe18:141c"
    ], 
    "ansible_apparmor": {
        "status": "disabled"
    }, 

Upvotes: 1

Sepehr Nazari
Sepehr Nazari

Reputation: 3875

Creating a new playbook with only one line:

- hosts: all

and running it collects all the facts, and if fact_caching is on, it will cache them for use in future playbooks with --limit.

Upvotes: 4

alfredocambera
alfredocambera

Reputation: 3420

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,

Upvotes: 48

Bruce P
Bruce P

Reputation: 20759

The absolute fastest way would be to make use of Ansible's fact caching that was introduced in version 1.8. It requires the use of a redis server to store facts, but it will let you store facts between playbook runs. As the documentation describes:

Imagine, for instance, a very large infrastructure with thousands of hosts. Fact caching could be configured to run nightly, but configuration of a small set of servers could run ad-hoc or periodically throughout the day. With fact-caching enabled, it would not be necessary to “hit” all servers to reference variables and information about them.

The problem with --limit is that it will limit all the hosts that you ansible will interact with, so it will limit even what special host group like all will impact. If you didn't use --limit but instead used host groups then you could do something like this:

---
- hosts: all
  tasks: []

- hosts: my_host_group
  tasks:
  - name: task1
    ...
  - name: task2
    ...

In this example, the first play would force the gathering of facts for all the hosts you have since you specified hosts: all. The second play would then perform the desired tasks on the hosts in the group my_host_group.

Upvotes: 0

Related Questions