Ansible: Automate Everything from First Playbook to Production · Variables, Facts and Jinja2 Templates
Gathering Facts: System Info and Custom Facts
Use Ansible facts to make playbooks adapt to each host's OS, memory, IP address and disk layout without hardcoding anything.
What you'll build
A playbook that gathers facts, makes decisions based on the OS family, and reads a custom fact you create on the managed host.
The idea
When a play starts with gather_facts: true (the default), Ansible runs the setup module on every target host. This populates hundreds of variables under the ansible_* namespace:
| Fact | Example value | Typical use |
|---|---|---|
ansible_hostname |
web-01 |
Log messages, file names |
ansible_os_family |
Debian or RedHat |
Choosing apt vs yum |
ansible_distribution |
Ubuntu |
Version-specific packages |
ansible_distribution_version |
22.04 |
Conditional logic |
ansible_default_ipv4.address |
10.0.1.42 |
Config file templates |
ansible_memtotal_mb |
4096 |
Sizing worker counts |
ansible_processor_vcpus |
2 |
Sizing thread pools |
You can filter the flood of facts when you only need a few, and you can create custom facts (local facts) by dropping JSON or INI files in /etc/ansible/facts.d/ on the managed host — Ansible merges them into ansible_local.
For large inventories, fact caching (JSON file or Redis) avoids gathering facts on every run, shaving minutes off playbook execution.
The code
mkdir -p facts-demo
cat > facts-demo/inventory.ini <<'EOF'
[local]
localhost ansible_connection=local
EOF
cat > facts-demo/show-facts.yml <<'EOF'
- name: Show useful facts
hosts: local
gather_facts: true
tasks:
- name: OS and hardware summary
ansible.builtin.debug:
msg: >-
{{ ansible_hostname }} runs {{ ansible_distribution }}
{{ ansible_distribution_version }}
({{ ansible_os_family }}) with {{ ansible_memtotal_mb }}MB RAM
and {{ ansible_processor_vcpus }} vCPUs
EOF
cd facts-demo
ansible-playbook -i inventory.ini show-facts.yml 2>/dev/null
Output
Run this on your computer — see “Get Started” for setup.
Filter facts from the command line:
cd facts-demo
ansible -i inventory.ini localhost -m ansible.builtin.setup -a 'filter=ansible_*memory*' 2>/dev/null
Output
Run this on your computer — see “Get Started” for setup.
Create a custom fact:
cd facts-demo
cat > facts-demo-custom.yml <<'EOF'
- name: Custom facts demo
hosts: local
gather_facts: false
tasks:
- name: Create facts directory
ansible.builtin.file:
path: /etc/ansible/facts.d
state: directory
mode: "0755"
become: true
- name: Deploy custom fact
ansible.builtin.copy:
dest: /etc/ansible/facts.d/app.fact
mode: "0644"
content: |
[release]
version=2.4.1
deployed_by=ci
environment=staging
become: true
- name: Re-gather facts to pick up custom fact
ansible.builtin.setup:
filter: ansible_local
- name: Show custom fact
ansible.builtin.debug:
msg: "App version {{ ansible_local.app.release.version }} deployed by {{ ansible_local.app.release.deployed_by }}"
EOF
ansible-playbook -i facts-demo/inventory.ini facts-demo-custom.yml 2>/dev/null
Output
Run this on your computer — see “Get Started” for setup.
Enable fact caching for faster subsequent runs by adding to ansible.cfg:
[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_fact_cache
fact_caching_timeout = 3600
With gathering = smart, Ansible only gathers facts when they are not already cached. On a 50-host inventory this can save 30–60 seconds per run.
Common mistakes
Assuming facts are available when gather_facts: false — if you disable fact gathering for speed, you cannot reference ansible_* variables. Either gather facts or hardcode the values you need.
Using ansible_eth0 instead of ansible_default_ipv4 — interface names vary between distros and cloud providers (eth0, ens5, enp0s3). The ansible_default_ipv4 fact always gives the primary interface regardless of name.
Not re-gathering after deploying custom facts — custom facts are read during gather_facts. If you deploy a .fact file mid-play, call ansible.builtin.setup again to pick it up.
Key points
gather_facts: true(the default) populates hundreds ofansible_*variables describing each host.- Filter facts with
ansible -m setup -a 'filter=...'or access only what you need in templates. - Custom facts live in
/etc/ansible/facts.d/and appear underansible_local. - Fact caching (
jsonfileorredis) avoids re-gathering on every run — essential for large inventories.
Practice questions
How do you get Ansible to show only memory-related facts for a host?
Answer
Run ansible <host> -m setup -a 'filter=ansible_*memory*' to filter facts by pattern.
Where do custom facts live on a managed host, and under what variable do they appear?
Answer
Custom facts are files in /etc/ansible/facts.d/. They appear under ansible_local.<filename>.
What does `gathering = smart` do in ansible.cfg?
Answer
It tells Ansible to use cached facts when available and only gather fresh facts when the cache is empty or expired.
Open in the app to mark lessons complete, take notes and track your progress.