# ansible / foundations / facts

Context-Aware Automation with Ansible Facts

Ansible Facts are variables automatically collected from your managed nodes before tasks are executed. They provide deep system insights—from IP addresses to CPU details—allowing your playbooks to adapt dynamically to the environment.

$ ansible all -m setup | grep os_family
Output"ansible_os_family": "Debian"
Usage{{ ansible_os_family }}
StatusData Collected
setup
Module
Dynamic
Variables
# intelligence

What are Ansible Facts?

Instead of hardcoding a server's IP address or manually checking if a server is RedHat or Ubuntu, Ansible queries the target host and builds a massive dictionary of variables called Facts.

dynamic

Why are they important?

Facts make automation context-aware. You can write highly reusable playbooks that behave differently based on the system they are running against. For example: "If OS is Debian, use apt; if RedHat, use yum."

data points

What data is collected?

Ansible collects hardware architecture, active IP addresses, hostname, total memory, disk space, network interfaces, and much more.

# execution

The Gathering Phase

The Execution Flow

  • 1. Connect

    Ansible connects to the remote server via SSH (or WinRM).

  • 2. Gather

    Ansible automatically runs the setup module behind the scenes to collect all system facts.

  • 3. Execute

    Ansible runs your defined tasks, substituting any fact variables (like {{ ansible_hostname }}) with the collected data.

Disabling Facts for SpeedYAML
1 2 3 4 5 6 7
---
- hosts: web
  gather_facts: no
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
PERFORMANCE

Does gathering facts take time? Yes. If you have hundreds of servers and your playbook doesn't rely on system variables, use gather_facts: no to speed up execution significantly.

# reference

Commands & Common Facts

You don't have to guess what facts are available. You can run an ad-hoc command using the setup module to dump the entire JSON payload of facts for any server.

terminal
# Manually view all collected facts from managed nodes
$ ansible all -m setup
server-01 | SUCCESS => {
    "ansible_facts": {
        "ansible_architecture": "x86_64",
        "ansible_hostname": "server-01",
        ...
Fact Variable (Jinja2) Description
{{ ansible_hostname }}The short hostname of the target server.
{{ ansible_os_family }}The OS family group (e.g., Debian, RedHat, Windows).
{{ ansible_default_ipv4.address }}The active, default IPv4 address of the server.
{{ ansible_processor }}Detailed information about the CPU architecture and cores.
{{ ansible_memtotal_mb }}The total physical RAM available on the machine (in MB).
# summary

Quick Recap

WHAT

Ansible Facts are automatically collected variables containing deep system information.

WHEN

Facts are gathered seamlessly at the start of a playbook run, right before the first task executes.

HOW

Disable them using gather_facts: no in your playbook, or explore them manually via the terminal using ansible all -m setup.