Ansible Curriculum
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:
The ansible_facts Dictionary vs. Old-Style Variables
In modern Ansible (2.5+), facts are stored inside the ansible_facts dictionary:
In older playbooks, facts were injected directly as top-level variables prefixed with ansible_:
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:
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.