What are Ansible Facts?
Ansible Facts are variables automatically collected from managed nodes (servers).
These facts provide detailed system information such as:
- IP Address
- Operating System
- CPU Details
- Memory Information
- Hostname
- Disk Information
- Network Interfaces
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
- Ansible connects to the server
- Ansible gathers system information (facts)
- Ansible runs the tasks
Why are Facts Important?
- Make automation dynamic
- Use system-specific values
- Write reusable playbooks
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
- Ansible Facts are automatically collected variables
- They contain system information
- Facts are gathered before tasks run
- Gathering facts takes a little time
- You can disable using gather_facts: no
- You can check facts using ansible all -m setup