Ansible Series

⚡ The Top 1% Hands-On for Speeding Up a Large Inventory by Caching Ansible Facts: What gather_facts Actually Costs

Introduction

This article is part of the Top 1% Series Complete Article Guide, the 9th article in the Ansible Series.

Prerequisite Knowledge

The Big Picture

This hands-on consists of four steps.

graph LR
    Step1["Step1<br/>Measure the time<br/>facts gathering takes"]
    Step2["Step2<br/>Configure a JSON<br/>file cache"]
    Step3["Step3<br/>Re-measure the<br/>second run's time"]
    Step4["Step4<br/>Tune the cache TTL<br/>for operation"]
    Step1 --> Step2 --> Step3 --> Step4

Hands-On Steps

Step 1: Measure the time facts gathering takes

With no cache, measure the execution time of a Playbook that only does facts gathering.

# facts_only.yml
- hosts: all
  gather_facts: true
  tasks:
    - name: Do nothing, just gather facts
      debug:
        msg: "Facts gathered"
time ansible-playbook -i inventory.ini facts_only.yml

With a handful of target hosts this takes only a few seconds, but this time grows nearly proportionally with the number of target hosts. The starting point of this hands-on is the fact that this cost is incurred, every single time, across hundreds of hosts, on every single Playbook run.

Step 2: Configure a JSON file cache

Add fact caching configuration to ansible.cfg.

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts_cache
fact_caching_timeout = 3600

Setting gathering = smart makes Ansible skip facts gathering entirely whenever the cache is valid and fresh. fact_caching_timeout is the number of seconds (here, 3600 seconds = 1 hour) for which the cache is considered valid.

Step 3: Re-measure the second run’s time

Run the same Playbook again and compare the execution time.

time ansible-playbook -i inventory.ini facts_only.yml

It should be dramatically shorter than the first run. Look inside the /tmp/ansible_facts_cache directory, and you’ll see each host’s facts content saved as a JSON file.

Step 4: Tune the cache TTL for operation

Try running it again once the cache has expired.

# Make the cache file's timestamp look expired
touch -d "2 hours ago" /tmp/ansible_facts_cache/*

time ansible-playbook -i inventory.ini facts_only.yml

Since the time specified in fact_caching_timeout has passed, facts gathering runs again, and the cache is refreshed with new content. Tuning this TTL value appropriately, based on how frequently the target hosts’ configuration actually changes, is the real-world key point.

What a Pro Sees Here (Top 1% Understanding)

What gather_facts actually does under the hood

gather_facts establishes an SSH connection to the target host and actually runs a Python script (the setup module), collecting hundreds of pieces of information — OS version, network interfaces, disk capacity, environment variables, and more. Without understanding that this process itself carries the far-from-trivial cost of establishing an SSH connection and running a Python script, it’s easy to fall into the misconception that “facts gathering should finish instantly.” As the host count grows, this cost accumulates, and facts gathering’s share of the Playbook’s total execution time becomes impossible to ignore.

How to weigh the risk of a cache going stale

A fact cache naturally carries the risk that “the cached information diverges from the target host’s actual current state.” For example, if a host’s disk was expanded during the cache period, a task referencing the stale cached disk information could make an incorrect judgment. The real-world answer to this risk is weighing “the heavy cost of an SSH connection on every facts gathering” against “the risk of a stale cache,” and tuning the TTL based on how frequently the target system’s configuration actually changes. Setting a short TTL for an environment whose configuration changes frequently, and a long TTL for one that barely changes, is the real-world way to strike this balance.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. Configured the cache but execution time didn’t shrink: Check whether gathering = smart is correctly set in ansible.cfg, and whether fact_caching_connection’s path has write permission.
  2. A task misbehaves referencing stale information: Check whether fact_caching_timeout’s value is too long relative to how frequently the target system’s configuration actually changes.
  3. One specific host needs the latest facts: Explicitly specify gather_facts: true for just that task, or clear the cache with meta: clear_facts.

Summary

Takeaways to Apply Today

  1. For a Playbook targeting more than a few dozen hosts, consider adopting a fact cache.
  2. Always factor in how frequently the target system’s configuration actually changes when setting a TTL.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).