Facts
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
setupmodule; - Prints the operating system family using the
debugmodule.
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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 8.33
Facts
Swipe to show menu
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
setupmodule; - Prints the operating system family using the
debugmodule.
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.
Thanks for your feedback!