Understanding Ansible Facts

Last updated: June 29, 2025

My Experience with Ansible Facts

When I first started working with Ansible, one of the most fascinating features I discovered was Ansible facts. These automatically gathered pieces of information about your managed systems are like gold for automation tasks. In my early days of automating mixed Linux and Windows environments, I often found myself manually gathering system information before writing playbooks. Once I understood the power of Ansible facts, my approach completely changed.

In this blog post, I'll share my experience with Ansible facts, including practical examples for both Linux and Windows environments, how to work with them effectively, and how to extend them with custom facts when needed.

What Are Ansible Facts?

Ansible facts are pieces of information about remote systems that Ansible automatically discovers when running playbooks. These facts contain details about the operating system, hardware, network interfaces, and much more. Think of facts as a comprehensive system inventory that's automatically collected and made available to you.

Ansible uses the setup module to gather these facts. By default, fact gathering happens automatically at the beginning of each play, making this information available to all your tasks.

Viewing Ansible Facts

My favorite way to understand what facts are available is to simply display them. You can view all facts for a host using this simple ad-hoc command:

# View all facts for a specific host
ansible hostname -m setup

# Save facts to a file for easier review
ansible hostname -m setup > facts.json

For Windows hosts, the same command works:

Core Facts Structure

Ansible facts are structured as nested JSON data. Here's a simplified view of what the structure looks like:

Accessing Facts in Playbooks

There are two main ways to access facts in your playbooks and templates:

  1. Using the ansible_facts dictionary:

  2. Using top-level variables with the ansible_ prefix (though this is becoming deprecated):

I personally prefer the first approach as it's more explicit and future-proof.

Useful Facts for Linux Systems

After working with Ansible for several years, I've found these Linux facts to be particularly useful:

Useful Facts for Windows Systems

Windows facts differ somewhat from Linux facts. Here are some Windows-specific facts I commonly use:

Filtering Facts

The full facts output can be overwhelming. In my experience, it's better to filter facts to get just what you need:

For complex filtering, I sometimes use regular expressions:

Facts Caching

One optimization I've found essential in large environments is fact caching. By default, Ansible gathers facts each time a playbook runs, which can be time-consuming for large inventories.

To enable fact caching, I add these lines to my ansible.cfg:

This dramatically speeds up repeated playbook runs, especially in environments with hundreds of hosts.

Custom Facts

Sometimes the built-in facts aren't enough. That's where custom facts come in. There are two main ways to create custom facts:

1. Using set_fact

For temporary facts within a playbook, I use set_fact:

2. Using facts.d (Permanent Custom Facts)

For permanent custom facts, I create files in the /etc/ansible/facts.d directory on managed hosts. These can be INI, JSON, or executable scripts that output JSON.

Here's a simple example of a custom fact file I've used for an application inventory:

I can then access these facts using ansible_facts['ansible_local']:

For Windows, the process is similar, but the facts.d directory is located at C:\ProgramData\Ansible\facts.d:

A Practical Facts Use Case: System-Specific Configuration

One of my favorite patterns is to use facts for dynamic configuration. Here's a real-world example I've used for configuring services based on system resources:

This ensures the application is configured optimally for each system's resources.

Sequence Diagram: Ansible Facts Collection Flow

Here's a visualization of how Ansible facts are gathered and used:

spinner

Performance Considerations

In large environments, fact gathering can become a performance bottleneck. Here are some strategies I've used to address this:

  1. Disable fact gathering when not needed:

  2. Use fact caching as mentioned earlier.

  3. Gather only specific fact subsets:

  4. Create host groups based on facts to streamline operations:

Conclusion

Ansible facts have been a cornerstone of my automation strategy. They provide the dynamic information needed to make playbooks adaptable to different environments and system configurations. Whether it's basic system information or complex custom data, facts provide a structured way to access this information.

In my experience, understanding and effectively using facts is what separates basic Ansible scripts from truly robust automation solutions. Once you master facts, your playbooks become more dynamic, more adaptable, and ultimately more valuable.

As I continue to automate increasingly complex environments, I find myself relying on facts more and more to make intelligent decisions within my playbooks. I hope sharing my experiences helps you leverage the full power of Ansible facts in your automation journey.

Last updated