What are Ansible facts and how does gathering work?
Learn what Ansible facts are, how the setup module gathers host data, how to use facts in conditionals, tune gather_subset, cache facts, and add custom facts.
Expected Interview Answer
Ansible facts are pieces of system information automatically discovered about each managed host — such as OS family, IP addresses, CPU, memory, and mounted disks — that you can use as variables inside playbooks.
At the start of a play, Ansible runs the setup module to gather facts and stores them under the ansible_facts namespace (also exposed as ansible_* variables). This lets tasks make decisions based on real host state, like choosing a package name by OS family. Gathering can be controlled with the play's `gather_facts` setting, tuned with `gather_subset`, sped up with a fact cache, and extended with custom facts placed in /etc/ansible/facts.d or set at runtime via set_fact.
- Enables OS- and hardware-aware conditional logic
- Provides ready-to-use variables like ansible_hostname and ansible_default_ipv4
- gather_subset and caching reduce overhead on large fleets
- Custom local facts extend discovery for app-specific data
- Reduces hardcoding by adapting playbooks to real host state
AI Mentor Explanation
Fact gathering is the ground inspection before a match. Before play, officials record the pitch condition, weather, boundary size, and outfield speed — objective facts about the venue. The captain then sets strategy from those readings, choosing spinners on a dry pitch. Ansible's setup step is that inspection: it measures each host's real state up front so the playbook can adapt its plan to what it actually finds.
Step-by-Step Explanation
Step 1
Play starts
By default Ansible runs the setup module against each host before any tasks execute.
Step 2
Facts collected
System data like ansible_os_family, ansible_default_ipv4, and ansible_memtotal_mb is gathered.
Step 3
Stored as variables
Facts live under ansible_facts and as ansible_* variables, usable in any task or template.
Step 4
Use in conditionals
Reference facts with `when:` or in Jinja2 to branch on OS, hardware, or network state.
Step 5
Tune gathering
Disable with gather_facts: false, narrow with gather_subset, or cache facts to cut overhead.
Step 6
Add custom facts
Drop scripts/INI in /etc/ansible/facts.d for local facts, or create runtime facts with set_fact.
What Interviewer Expects
- Defines facts as auto-discovered host information
- Knows the setup module gathers them at play start
- Can name common facts like ansible_os_family or ansible_default_ipv4
- Understands gather_facts, gather_subset, and fact caching
- Aware of custom local facts and set_fact
Common Mistakes
- Confusing facts with user-defined variables
- Forgetting to enable gathering when a fact is referenced
- Not knowing facts can be cached for performance
- Assuming set_fact values are auto-gathered system facts
- Ignoring gather_subset and gathering everything on huge fleets
Best Answer (HR Friendly)
“Ansible facts are details Ansible automatically discovers about each computer it manages — things like the operating system, memory, and network address. Ansible collects them at the start of a run so your automation can make smart, machine-specific decisions instead of assuming.”
Code Example
- hosts: all
gather_facts: true
tasks:
- name: Install Apache on Debian family
ansible.builtin.apt:
name: apache2
state: present
when: ansible_facts['os_family'] == 'Debian'
- name: Install Apache on RedHat family
ansible.builtin.yum:
name: httpd
state: present
when: ansible_facts['os_family'] == 'RedHat'- hosts: web
gather_facts: true
gather_subset:
- "network"
- "!hardware"
tasks:
- name: Show default IPv4
ansible.builtin.debug:
msg: "{{ ansible_default_ipv4.address }}"
- name: Define a runtime fact
ansible.builtin.set_fact:
deploy_tier: "frontend"Follow-up Questions
- How do you disable fact gathering and why would you?
- What is fact caching and which backends can store it?
- How do custom local facts in facts.d work?
- What is the difference between a fact and a set_fact variable?
- How does gather_subset reduce gathering time?
MCQ Practice
1. Which module gathers facts about managed hosts?
The setup module runs automatically at play start (unless disabled) and collects host facts.
2. Where are gathered facts primarily stored?
Facts are exposed under the ansible_facts namespace and as ansible_* variables for use in tasks and templates.
3. What does setting gather_facts: false achieve?
Disabling gather_facts skips the setup module, which speeds plays that don't need discovered facts.
Flash Cards
What are Ansible facts? — Auto-discovered host information (OS, IP, CPU, memory) collected by the setup module and usable as variables.
Where are facts stored? — Under the ansible_facts namespace and as ansible_* variables.
How do you speed up gathering? — Use gather_subset to limit categories, enable fact caching, or disable gathering when not needed.
What is a custom local fact? — A script or INI file in /etc/ansible/facts.d that adds host-specific facts under ansible_local.