AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

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 ArticlesAnsible Magic Variables: Special Variables ReferenceAnsible Variables: Complete GuideAnsible Hostname vs Inventory Hostname

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home