Ansible Facts and Gathering Overhead
Facts are what a managed node knows about itself, and Ansible collects them before every play unless told not to. That is a real cost, measured here at more than three times the runtime of a play that skips it. This covers what a fact is, the handful you will use, what turning gathering off breaks, and how to publish your own facts from a file on the host.
Plays and Playbooks Guide 12 of 45 Intermediate
- OSUbuntu 26.04 LTS
- ansible-core2.20.1
- Python3.14.4
- TimeAbout 15 min
Facts are being renamed. Running a play that uses ansible_hostname now prints a deprecation warning: top-level fact injection is going away in ansible-core 2.24, and the supported form is ansible_facts['hostname'] - the same value without the ansible_ prefix, inside the ansible_facts dictionary. New playbooks should use the dictionary form.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| ANS-CTL01 | 192.168.0.36 | Ubuntu 26.04 LTS | Ansible Control Node | 2 Core | 3 GB | 50 GB |
| ANS-A01 | 192.168.0.37 | Ubuntu 26.04 LTS | Managed Node (group: web) | 2 Core | 3 GB | 50 GB |
| ANS-B01 | 192.168.0.38 | Ubuntu 26.04 LTS | Managed Node (group: db) | 2 Core | 3 GB | 50 GB |
This guide includes
Use this when a playbook is slower than it ought to be. This matters because Ansible collects several hundred values from every host before your first task runs - and turning that off takes the variables with it.
- seeing what a fact is, and how many the setup module returns
- using the five facts you will actually reference
- measuring what gathering costs, against the same playbook with it off
- finding that without facts those variables are simply gone
- adding facts of your own under
/etc/ansible/facts.d/
Before you start
- A control node configured as shown in the first step.
- The session writes a custom fact to
/etc/ansible/facts.d/on thewebhost and removes it at the end.
-
The configuration this guide assumes
-
What a fact is
-
The ones you will actually use
-
What gathering costs
-
Without facts, the variables are gone
-
Your own facts