Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Facts | Managing Systems with Ansible
Introduction to Ansible

bookFacts

What Are Facts in Ansible?

Facts in Ansible are pieces of information automatically collected about your managed systems. When you run an Ansible playbook, Ansible gathers facts such as the operating system, network interfaces, IP addresses, memory, disk space, and hardware details. These facts are stored as variables and can be used throughout your automation tasks.

Why Facts Are Useful in Automation

  • Provide up-to-date details about each system;
  • Allow you to make decisions based on system properties, such as choosing the correct package for the operating system;
  • Enable dynamic playbooks that adapt to different environments;
  • Help you audit and document your infrastructure automatically.

Facts make your automation smarter and more flexible by letting you tailor tasks to the specific characteristics of each machine.

Gathering Facts

The setup module is responsible for gathering these facts. By default, Ansible runs the setup module at the start of every play unless you disable it. You can also use the setup module explicitly to gather facts at any point in your playbook or to filter for specific information.

Using the setup Module

To collect all available facts from a target host, include a task like this in your playbook:

- name: Gather all facts from the target host
  ansible.builtin.setup:

This task collects and displays all facts about the system, such as ansible_hostname, ansible_distribution, and ansible_memtotal_mb.

Simple Example

The following playbook gathers facts from a group of hosts and prints the operating system name for each:

- name: Gather and display OS information
  hosts: all
  tasks:
    - name: Gather system facts
      ansible.builtin.setup:

    - name: Show operating system
      debug:
        msg: "The operating system is {{ ansible_facts['os_family'] }}"

This playbook:

  • Collects facts using the setup module;
  • Prints the operating system family using the debug module.

You can use any collected fact in your tasks by referencing it with the ansible_facts variable.

Using Facts in Playbooks

Ansible automatically gathers useful system information called facts during playbook execution. You can use these facts to customize your tasks. Here is a simple example that prints the system's hostname:

---
- name: Print the hostname using Ansible facts
  hosts: all
  tasks:
    - name: Show the hostname
      debug:
        msg: "The hostname is {{ ansible_hostname }}"
  • ---: indicates the start of the YAML document;
  • - name: Print the hostname using Ansible facts: describes the purpose of the play;
  • hosts: all: runs the play on all targeted hosts;
  • tasks:: defines a list of actions to perform;
  • - name: Show the hostname: describes the task;
  • debug:: uses the built-in debug module to print a message;
  • msg: "The hostname is {{ ansible_hostname }}": uses the ansible_hostname fact to display the current host's name.
question mark

Which variable should you use to access all gathered facts in an Ansible playbook?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

What are some common facts that Ansible collects by default?

How can I disable fact gathering in a playbook?

Can I gather only specific facts instead of all available ones?

Awesome!

Completion rate improved to 8.33

bookFacts

Свайпніть щоб показати меню

What Are Facts in Ansible?

Facts in Ansible are pieces of information automatically collected about your managed systems. When you run an Ansible playbook, Ansible gathers facts such as the operating system, network interfaces, IP addresses, memory, disk space, and hardware details. These facts are stored as variables and can be used throughout your automation tasks.

Why Facts Are Useful in Automation

  • Provide up-to-date details about each system;
  • Allow you to make decisions based on system properties, such as choosing the correct package for the operating system;
  • Enable dynamic playbooks that adapt to different environments;
  • Help you audit and document your infrastructure automatically.

Facts make your automation smarter and more flexible by letting you tailor tasks to the specific characteristics of each machine.

Gathering Facts

The setup module is responsible for gathering these facts. By default, Ansible runs the setup module at the start of every play unless you disable it. You can also use the setup module explicitly to gather facts at any point in your playbook or to filter for specific information.

Using the setup Module

To collect all available facts from a target host, include a task like this in your playbook:

- name: Gather all facts from the target host
  ansible.builtin.setup:

This task collects and displays all facts about the system, such as ansible_hostname, ansible_distribution, and ansible_memtotal_mb.

Simple Example

The following playbook gathers facts from a group of hosts and prints the operating system name for each:

- name: Gather and display OS information
  hosts: all
  tasks:
    - name: Gather system facts
      ansible.builtin.setup:

    - name: Show operating system
      debug:
        msg: "The operating system is {{ ansible_facts['os_family'] }}"

This playbook:

  • Collects facts using the setup module;
  • Prints the operating system family using the debug module.

You can use any collected fact in your tasks by referencing it with the ansible_facts variable.

Using Facts in Playbooks

Ansible automatically gathers useful system information called facts during playbook execution. You can use these facts to customize your tasks. Here is a simple example that prints the system's hostname:

---
- name: Print the hostname using Ansible facts
  hosts: all
  tasks:
    - name: Show the hostname
      debug:
        msg: "The hostname is {{ ansible_hostname }}"
  • ---: indicates the start of the YAML document;
  • - name: Print the hostname using Ansible facts: describes the purpose of the play;
  • hosts: all: runs the play on all targeted hosts;
  • tasks:: defines a list of actions to perform;
  • - name: Show the hostname: describes the task;
  • debug:: uses the built-in debug module to print a message;
  • msg: "The hostname is {{ ansible_hostname }}": uses the ansible_hostname fact to display the current host's name.
question mark

Which variable should you use to access all gathered facts in an Ansible playbook?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4
some-alt