Ansible Facts: Gather, Use, and Create Custom Facts (Complete Guide)
By Luca Berton · Published 2024-01-01 · Category: installation
How to use Ansible facts to gather system information. ansible_facts, setup module, custom facts, fact caching. Access hostname, IP, OS, memory with playbook examples.
Ansible Facts: Gather, Use, and Create Custom Facts (Complete Guide)
Ansible facts are system information automatically gathered from managed hosts — OS, IP addresses, hostname, memory, disk, network interfaces, and more. They're available as variables in your playbooks.
How Facts Work
When a playbook runs, Ansible calls the setup module on each host to collect facts before executing tasks.
Common Facts Reference
System Information
Network
Memory and CPU
Disk
Date and Time
Access Facts
New Style (ansible_facts dictionary)
Classic Style (flat variables)
Both styles work. The ansible_facts dictionary is the modern approach.
View All Facts
Use Facts in Conditionals
Use Facts in Templates
Custom Facts (Local Facts)
Create custom facts by placing .fact files on managed hosts.
JSON Custom Fact
INI Custom Fact
Executable Custom Fact
Deploy and Use Custom Facts
Disable Fact Gathering
Fact Caching
Cache facts to avoid re-gathering on every run.
FAQ
What are Ansible facts?
Ansible facts are system information automatically gathered from managed hosts — OS, IP addresses, hostname, CPU, memory, disk mounts, and network interfaces. They're available as variables in playbooks and templates.
How do I see all Ansible facts for a host?
Run ansible hostname -m ansible.builtin.setup to display all facts. Use -a "filter=ansible_distribution*" to filter specific facts. In a playbook, use debug: var=ansible_facts.
How do I get the hostname in Ansible?
Use ansible_hostname for the short hostname or ansible_fqdn for the fully qualified domain name. For the inventory hostname, use inventory_hostname.
What is the difference between ansible_hostname and inventory_hostname?
ansible_hostname is the actual system hostname gathered from the host. inventory_hostname is the name defined in your inventory file. They may differ if the inventory uses IPs or aliases.
How do I create custom Ansible facts?
Place .fact files (JSON, INI, or executable scripts) in /etc/ansible/facts.d/ on the managed host. Access them via ansible_local.filename.key. Use the setup module with filter=ansible_local to read them.
Can I disable fact gathering?
Yes, set gather_facts: false in your play. This speeds up execution when facts aren't needed. You can also use gather_subset to collect only specific fact categories.
Conclusion
Facts give your playbooks runtime awareness of each host's configuration. Use them for conditionals, templates, and dynamic configuration. Create custom facts for application-specific metadata and enable fact caching for performance.
Related Articles • Ansible Magic Variables: Special Variables Reference • Ansible Variables: Complete Guide • Ansible Hostname vs Inventory Hostname
Category: installation