ANSIBLE FACTS

What are Ansible Facts?

Ansible Facts are variables automatically collected from managed nodes (servers).

These facts provide detailed system information such as:

Ansible collects this information automatically before executing tasks.

When are Facts Collected?

When you run a playbook, Ansible collects facts before running any tasks.

This step is called Gathering Facts.

Execution Flow

  1. Ansible connects to the server
  2. Ansible gathers system information (facts)
  3. Ansible runs the tasks

Why are Facts Important?

Example

Instead of hardcoding IP address, you can use:

{{ ansible_default_ipv4.address }}
        

Does Gathering Facts Take Time?

Yes. Gathering facts takes a small amount of time because Ansible collects system details.

If you don't need facts, you can disable it to make playbooks run faster.

Example

- hosts: web
  gather_facts: no
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
        

How to Check Facts Using Ad-Hoc Command

You can view facts using the setup module:

ansible all -m setup
        

This command displays all collected facts from managed nodes.

Simple Definition

Ansible Facts = Automatically collected system information stored as variables

Example Fact Variables

Fact Variable Description
ansible_hostname Hostname of the server
ansible_os_family OS type (Debian, RedHat, etc.)
ansible_default_ipv4.address Server IP address
ansible_processor CPU information
ansible_memtotal_mb Total memory in MB

Summary