Ansible facts are a cornerstone of Ansible automation, providing valuable information about managed nodes that can be dynamically used in playbooks. This article explains what facts are, how they work, and their role in enhancing automation workflows.

What Are Ansible Facts?

Ansible facts are system variables automatically collected from managed nodes during playbook execution. They include details about the system’s hardware, operating system, network interfaces, and more. These facts enable playbooks to adapt dynamically to different environments.

Key Features of Facts:

  • Automatically Gathered: By default, Ansible collects facts using the setup module at the start of a playbook run.
  • Dynamic Variables: Facts provide real-time information about target systems.
  • Versatile Usage: Use facts in conditions, tasks, templates, and handlers.

How to Use Ansible Facts

1. Accessing Facts

Facts are accessible as variables in your playbooks. For example:

- name: Print operating system
  debug:
    msg: "The OS is {{ ansible_os_family }}"

2. Common Facts

  • OS Information:
    {{ ansible_distribution }} - Operating system name
    {{ ansible_distribution_version }} - OS version
    
  • Network Details:
    {{ ansible_default_ipv4.address }} - Default IPv4 address
    {{ ansible_fqdn }} - Fully qualified domain name
    
  • Hardware Info:
    {{ ansible_processor }} - CPU details
    {{ ansible_memtotal_mb }} - Total memory in MB
    

3. Using Facts in Conditions

Facts are often used in when statements to create conditional tasks:

- name: Install Apache on Debian systems
  apt:
    name: apache2
    state: present
  when: ansible_os_family == "Debian"

4. Custom Facts

Define your own facts by placing files in /etc/ansible/facts.d/ on managed nodes. For example:

echo '{"custom_fact": "value"}' > /etc/ansible/facts.d/custom.json

Access this custom fact in your playbook:

- name: Print custom fact
  debug:
    msg: "Custom fact value: {{ ansible_local.custom_fact }}"

Controlling Fact Gathering

1. Disabling Fact Gathering

To skip automatic fact gathering, set gather_facts: no in your playbook:

- name: Play without facts
  hosts: all
  gather_facts: no

2. Gather Specific Facts

Use the gather_subset option to limit the scope of facts:

- name: Gather minimal facts
  setup:
    gather_subset:
      - network
      - hardware

3. Gathering Facts On-Demand

Manually gather facts at any point using the setup module:

- name: Re-gather facts
  setup:

Best Practices for Using Facts

  • Filter Facts: Avoid unnecessary overhead by gathering only required facts using gather_subset.
  • Use Custom Facts: Create custom facts for environment-specific information.
  • Secure Data: Encrypt sensitive facts using Ansible Vault.
  • Debug Facts: Use the setup module with filters to explore available facts:
    ansible localhost -m setup -a "filter=ansible_*"
    

Common Use Cases for Ansible Facts

  1. Dynamic Configuration: Adjust configurations based on system properties, such as memory or OS.

  2. Inventory Grouping: Use facts to dynamically group hosts in playbooks.

  3. Conditional Execution: Ensure tasks are executed only when certain conditions are met.

Conclusion

Ansible facts play a vital role in making automation intelligent and adaptive. By leveraging facts, you can create flexible and efficient playbooks that respond dynamically to the state of managed systems.

Learn More About Ansible Facts

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Explore practical examples of using facts in Ansible by Examples.

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Support this project by contributing today.

Patreon Buy me a Pizza