How to configure the gather_facts option in an Ansible playbook

AnsibleAnsibleBeginner
Practice Now

Introduction

Ansible, a powerful IT automation tool, provides the gather_facts option to collect system information during playbook execution. Understanding how to configure this option is crucial for optimizing your Ansible workflows. This tutorial will guide you through the process of configuring the gather_facts option and explore practical use cases to help you get the most out of your Ansible deployments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL ansible(("`Ansible`")) -.-> ansible/ModuleOperationsGroup(["`Module Operations`"]) ansible(("`Ansible`")) -.-> ansible/PlaybookEssentialsGroup(["`Playbook Essentials`"]) ansible/ModuleOperationsGroup -.-> ansible/debug("`Test Output`") ansible/PlaybookEssentialsGroup -.-> ansible/playbook("`Execute Playbook`") subgraph Lab Skills ansible/debug -.-> lab-414866{{"`How to configure the gather_facts option in an Ansible playbook`"}} ansible/playbook -.-> lab-414866{{"`How to configure the gather_facts option in an Ansible playbook`"}} end

Understanding Ansible's gather_facts

Ansible is a powerful automation tool that allows you to manage and configure your infrastructure. One of the key features of Ansible is its ability to gather facts about the target hosts, which can be used to make informed decisions in your playbooks.

The gather_facts option in Ansible is a built-in module that collects a variety of information about the target hosts, such as operating system, CPU, memory, network interfaces, and more. This information can be accessed and used in your playbooks to customize the deployment or configuration process.

By default, Ansible will gather facts about the target hosts at the beginning of each playbook run. However, in some cases, you may want to disable or customize the fact gathering process, depending on your specific requirements.

graph TD A[Playbook Execution] --> B[Fact Gathering] B --> C[Task Execution] C --> D[Playbook Completion]

The gathered facts are stored in the ansible_facts dictionary, which can be accessed and used in your playbook tasks. For example, you can use the ansible_facts to determine the operating system of the target host and perform different actions based on that information.

- hosts: all
  tasks:
    - name: Print the operating system
      debug:
        msg: "The target host is running {{ ansible_facts['distribution'] }} {{ ansible_facts['distribution_version'] }}"

By understanding the gather_facts option and how to use the collected information, you can write more efficient and flexible Ansible playbooks that can adapt to different environments and requirements.

Configuring the gather_facts option

Disabling fact gathering

To disable the fact gathering process, you can set the gather_facts option to false in your playbook:

- hosts: all
  gather_facts: false
  tasks:
    ## Your tasks here

By setting gather_facts: false, Ansible will skip the fact gathering step and proceed directly to the task execution.

Selective fact gathering

In some cases, you may only need a subset of the available facts. Ansible allows you to selectively gather facts by using the gather_subset option:

- hosts: all
  gather_subset:
    - "!all"
    - "minimal"
  tasks:
    ## Your tasks here

In the example above, Ansible will only gather the "minimal" set of facts, which includes basic information about the target host, such as the operating system and architecture.

You can customize the gather_subset option to include or exclude specific fact groups, such as hardware, network, or virtual.

Dynamic fact gathering

Ansible also supports dynamic fact gathering, where the facts are gathered only when they are needed in the playbook tasks. This can be useful to reduce the overall execution time of your playbook.

To enable dynamic fact gathering, you can use the when clause to conditionally gather facts:

- hosts: all
  tasks:
    - name: Print the operating system
      debug:
        msg: "The target host is running {{ ansible_facts['distribution'] }} {{ ansible_facts['distribution_version'] }}"
      when: ansible_facts['distribution'] is defined

In this example, the when clause checks if the ansible_facts['distribution'] variable is defined before attempting to use it in the debug task. This ensures that the facts are only gathered when they are needed.

By understanding these options, you can optimize the fact gathering process in your Ansible playbooks to suit your specific requirements.

Practical use cases for gather_facts

The gather_facts option in Ansible has several practical use cases that can help you streamline your automation workflows. Here are a few examples:

Conditional task execution

You can use the gathered facts to conditionally execute tasks based on the target host's characteristics. For instance, you can install different packages or configure different services depending on the operating system or architecture of the target host.

- hosts: all
  tasks:
    - name: Install package on Ubuntu
      apt:
        name: htop
        state: present
      when: ansible_facts['distribution'] == 'Ubuntu'

    - name: Install package on CentOS
      yum:
        name: htop
        state: present
      when: ansible_facts['distribution'] == 'CentOS'

Dynamic variable assignment

The gathered facts can be used to dynamically assign variables in your playbooks, making them more flexible and adaptable to different environments.

- hosts: all
  vars:
    web_server_port: "{{ ansible_facts['port'] }}"
  tasks:
    - name: Start web server
      service:
        name: apache2
        state: started
        port: "{{ web_server_port }}"

Inventory management

You can use the gathered facts to enhance your Ansible inventory, such as grouping hosts based on specific characteristics or adding dynamic metadata to your hosts.

- hosts: all
  tasks:
    - name: Add host to dynamic group based on OS
      add_host:
        name: "{{ inventory_hostname }}"
        groups: "{{ ansible_facts['distribution'] }}"

Reporting and auditing

The gathered facts can be used to generate reports or perform audits on your infrastructure, providing valuable insights into the state of your systems.

- hosts: all
  tasks:
    - name: Gather system information
      setup:

    - name: Generate system report
      template:
        src: system_report.j2
        dest: /path/to/report/{{ inventory_hostname }}.txt

By understanding and leveraging the gather_facts option, you can create more powerful and versatile Ansible playbooks that can adapt to a wide range of scenarios and requirements.

Summary

In this comprehensive Ansible tutorial, you will learn how to effectively configure the gather_facts option in your playbooks. By understanding the purpose and practical applications of the gather_facts option, you will be able to optimize the performance and control the data collection process in your Ansible-driven infrastructure. Whether you're a seasoned Ansible user or just starting out, this guide will equip you with the knowledge to make the most of Ansible's powerful fact gathering capabilities.

Other Ansible Tutorials you may like