- Architecture (e.g., 32-bit vs 64-bit)
- Operating system version
- Processor and memory specifications
- Network interfaces, IP addresses, FQDN, and MAC addresses
- Disk details
The collected data is stored in the variable
ansible_facts, which can be used in subsequent tasks to tailor configurations and decisions based on system characteristics.Simple Playbook Example
Consider the following playbook that prints a simple hello message. Even though only the debug task is specified in the playbook, Ansible first gathers facts from each host:Displaying Ansible Facts
To gain deeper insights, you can modify your playbook to print theansible_facts variable instead of a simple message. This approach allows you to view extensive system details for each host:
ansible_facts can be invaluable when configuring systems dynamically—whether you are setting up logical volumes, managing network settings, or optimizing system performance based on the hardware characteristics of your servers.
Disabling Fact Gathering
If your playbook does not require this additional overhead of gathering facts, you can disable it by setting thegather_facts option to no:
gather_facts: no, Ansible skips the facts collection phase and executes only the specified tasks. Note that fact-gathering behavior can be further controlled by the setting in the Ansible configuration file (typically located at /etc/ansible/ansible.cfg):
Targeted Fact Gathering
Remember that Ansible collects facts only for the hosts included in the playbook. For example, if your inventory has two hosts (web1 and web2) but you run the playbook only on web1, facts will be gathered solely for web1:By understanding and leveraging Ansible Facts, you can create more efficient, responsive, and tailored automation scripts that adapt based on the actual state and configuration of your systems.