Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Fact Caching in Ansible

Introduction

Fact caching in Ansible is a performance tuning technique used to store facts gathered from managed nodes, allowing subsequent playbook runs to retrieve these facts from the cache instead of gathering them again. This can significantly improve the speed of your playbook executions, especially in large environments.

Why Use Fact Caching?

Fact caching is beneficial because it:

  • Reduces the time required to gather facts from managed nodes.
  • Minimizes the load on managed nodes by avoiding repeated fact-gathering operations.
  • Improves the overall performance and efficiency of your Ansible playbooks.

Setting Up Fact Caching

To enable fact caching, you need to configure your Ansible control node. The most commonly used fact cache plugins are `jsonfile` and `redis`. Below, we'll go through the steps to set up fact caching using the `jsonfile` plugin.

Edit your Ansible configuration file (usually located at /etc/ansible/ansible.cfg) and add the following settings:

[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_cache
fact_caching_timeout = 86400
                

In this configuration:

  • fact_caching: Specifies the plugin to use for caching facts.
  • fact_caching_connection: Defines the directory where the cache files will be stored.
  • fact_caching_timeout: Sets the duration (in seconds) for which the cached facts are considered valid.

Using Fact Caching in Playbooks

Once fact caching is enabled, Ansible will automatically use the cache for fact gathering. Here’s an example of a simple playbook that demonstrates this:

---
- name: Example Playbook with Fact Caching
  hosts: all
  gather_facts: yes
  tasks:
    - name: Print Ansible Facts
      debug:
        var: ansible_facts
                

When you run this playbook, Ansible will gather facts during the first run and store them in the cache. Subsequent runs will retrieve the facts from the cache, speeding up the execution.

Verifying Fact Caching

To verify that fact caching is working, you can check the cache directory specified in the configuration. For the `jsonfile` plugin, you should see JSON files created in the specified directory (/tmp/ansible_cache). These files contain the cached facts for each managed node.

$ ls /tmp/ansible_cache
node1.json  node2.json  node3.json
                

This output indicates that facts for node1, node2, and node3 have been cached.

Clearing the Fact Cache

If you need to clear the fact cache, you can simply delete the cache files from the specified directory. Alternatively, you can use the setup module with the --flush-cache option to clear the cache for a specific host.

$ ansible all -m setup --flush-cache
                

This command will clear the fact cache for all hosts in your inventory.

Conclusion

Fact caching is a powerful feature in Ansible that can help you optimize the performance of your playbooks by reducing the time spent on gathering facts. By setting up and using fact caching, you can achieve faster and more efficient playbook executions, especially in environments with a large number of managed nodes.