Subtopic•Ansible Facts

What Are Facts

When Ansible runs a play, before executing any of your defined tasks, it runs a hidden first task called Gathering Facts. This task executes the built-in setup module on every target host, collects detailed system metadata, and stores the results in a special variable dictionary called ansible_facts.

You can reference these facts inside your playbook tasks to make decisions based on the host's actual system configuration — without needing to define values manually.

In this guide, we will cover what facts are collected, how they are stored, how to access them, and why they are important for cross-platform automation.

What Facts Does Ansible Collect?

Facts are organized into logical categories:

CategorySample VariablesDescription
OS Informationansible_facts['os_family'], ansible_facts['distribution']Operating system family and version
Hardwareansible_facts['memtotal_mb'], ansible_facts['processor_count']RAM capacity and CPU details
Networkansible_facts['default_ipv4']['address'], ansible_facts['interfaces']IP addresses and network interfaces
Storageansible_facts['mounts'], ansible_facts['devices']Mounted filesystems and disk devices
Pythonansible_facts['python']['version']Python interpreter details
Timeansible_facts['date_time']['date']Server's current date and time

The ansible_facts Dictionary vs. Old-Style Variables

In modern Ansible (2.5+), facts are stored inside the ansible_facts dictionary:

# Modern (Recommended)
ansible_facts['os_family']
ansible_facts['memtotal_mb']

In older playbooks, facts were injected directly as top-level variables prefixed with ansible_:

# Legacy (Still functional but less explicit)
ansible_os_family
ansible_memtotal_mb

Both methods work, but the modern ansible_facts dictionary notation is preferred in current playbooks because it clearly signals that a value was collected automatically by Ansible.


1. Viewing All Available Facts

To see every fact collected for a host, run the setup module ad-hoc:

# Print all facts collected from the webservers group
ansible webservers -m ansible.builtin.setup

# Filter facts by a pattern keyword
ansible webservers -m ansible.builtin.setup -a "filter=ansible_memory*"

The output is a structured JSON dictionary. You can use this output to identify the exact variable keys available for your conditional tasks.


Summary

Ansible facts are automatically collected system metadata stored in the ansible_facts dictionary. They enable you to write playbooks that adapt to each host's actual hardware, OS, and network configuration. In the next section, we will cover gathering facts in depth.

←Previous ModuleVariables