Hey Techies…👋
In this blog, we’re going to explore Ansible facts : to gather insights into various aspects of the target hosts.
Ansible facts are instrumental for sysadmins, offering the ability to fine-tune playbook execution based on real-time system information. These dynamic variables empower administrators to make informed decisions and tailor automation processes to the specific conditions of each system.
Ansible facts serve as the host-specific system insights that you tap into during your connections. They encompass a wealth of data — from IP addresses and BIOS details to software and hardware specifics. By leveraging Ansible facts, admins gain the upper hand in host management, ensuring actions are taken based on the system’s real-time condition. It’s about informed decision-making and adapting to the unique characteristics of each system for a smoother admin experience.
Key points about Ansible facts include:
- Automatic Gathering: Ansible automatically collects facts using the setup module at the beginning of each playbook run.
- Information Variety: Facts cover a wide range of information, including system details (OS, architecture), hardware specifics, network configuration, available memory, and more.
- Accessing Facts: Facts are stored in the ansible_facts dictionary and can be accessed in playbooks using the syntax ansible_facts[‘fact_name’] or simply ansible_facts.fact_name.
- Dynamic Variables: Ansible facts act as dynamic variables that can be used to customize playbook behavior based on the real-time conditions of the target systems.
- Use Cases: Facts are valuable for conditionally executing tasks, customizing configurations, or performing actions based on the characteristics of each managed host.
Below is an example playbook that installs the Apache web server on multiple Linux hosts, considering the differences in package names: In this scenario, Ansible can intelligently handle the variations across different Linux distributions and package managers using its modular design.
---
- name: Example Playbook
hosts: your_target_hosts
gather_facts: true # This is the default, but explicitly mentioning it here for clarity
tasks:
- package:
name: "httpd"
state: present
when ansible_facts["os_name"] == "RedHat"
- package:
name: "apache2"
state: present
when ansible_facts["os_name"] == "Ubuntu"
- Check Linux Distribution: The setup module gathers facts about the system, specifically the distribution information.
- Install Apache on RHEL-based systems: If the distribution is Red Hat, the yum module installs the httpd package.
- Install Apache on Ubuntu-based systems: If the distribution is Ubuntu, the apt module installs the apache2 package.
Ansible facts are gathered using the setup module, and by default, Ansible executes this module at the beginning of each playbook run. The information collected by the setup module forms a set of variables known as facts, providing comprehensive details about the target hosts. This dynamic data allows Ansible playbooks to adapt and make informed decisions based on the current state of the systems they are managing. It’s a key mechanism for achieving flexibility and automation intelligence in Ansible workflows.
Access Ansible facts using ad-hoc commands
To access Ansible facts using ad-hoc commands, you can use the ansible command with the -m setup module. This module gathers facts about remote hosts. Here’s an example:
ansible all -m setup
This command collects facts for all hosts in your inventory. If you want facts for a specific host or group of hosts, replace all with the host or group name.
For example, to collect facts for a host named webserver:
ansible webserver -m setup
The output will include a large amount of information about the target hosts, including details about the hardware, operating system, network interfaces, and more.
You can also filter the facts to display specific information. For instance, to only show facts related to the distribution, you can use:
ansible all -m setup -a 'filter=ansible_distribution*'
Access the variables from Ansible facts in the Ansible playbook
In Ansible playbooks, you can access the variables from Ansible facts using the ansible_facts dictionary. Ansible facts are automatically available to your playbook, and you can reference them directly.
---
- name: Example Playbook
hosts: your_target_hosts
gather_facts: true # This is the default, but explicitly mentioning it here for clarity
tasks:
- name: Display system information
debug:
msg: "System: {{ ansible_facts['system'] }}"
The gather_facts: true line ensures that Ansible gathers facts before running tasks.
The ansible_facts[‘system’] is used to access the system variable from Ansible facts.
If there are specific variables where you’re encountering issues, it could be related to the structure of the facts or specific details about the data you’re trying to access. Always refer to the Ansible documentation or use the debug module to inspect the gathered facts and their structure.
Ansible facts provide a wealth of information about the target hosts, and their data types can vary depending on the type of information being represented.
Here are some common Ansible facts and their potential data types:
ansible_facts[‘ansible_distribution’]
- Data Type: String
- Example: ‘Ubuntu’
ansible_facts[‘ansible_kernel’]
- Data Type: String
- Example: ‘4.15.0–74-generic’
ansible_facts[‘ansible_processor_count’]
- Data Type: Integer
- Example: 8
ansible_facts[‘ansible_memory_mb’]
- Data Type: Dictionary
- Example:
ansible_memory_mb:
real:
total: 16000
free: 8000
ansible_facts[‘ansible_date_time’]
- Data Type: Dictionary
- Example:
ansible_date_time:
date: '2023-01-01'
time: '12:34:56'
ansible_facts[‘ansible_interfaces’]
- Data Type: Dictionary
- Example:
ansible_interfaces:
eth0:
ipv4:
address: '192.168.1.100'
These are just a few examples, and the actual data types may vary based on the specific fact. It’s important to refer to the Ansible documentation or use the debug module to inspect the structure and data types of Ansible facts for precise information about a particular variable.
Let’s look at a simple example playbook that gathers facts using the setup module and then prints some of the gathered information:
---
- name: Gather Facts Example
hosts: your_target_hosts
gather_facts: true # This is the default, but explicitly mentioning it here for clarity
tasks:
- name: Display Hostname
debug:
var: ansible_hostname
- name: Display OS Family
debug:
var: ansible_facts['ansible_os_family']
- name: Display Memory Information
debug:
var: ansible_facts['ansible_memory_mb']
- name: Display Network Interfaces
debug:
var: ansible_facts['ansible_interfaces']
In this example:
- The playbook gathers facts automatically because gather_facts is set to true.
- The debug module is used to print specific facts, such as hostname, OS family, memory information, and network interfaces.
Run this playbook with your target hosts, and it will display information gathered by Ansible about the specified hosts. You can tailor the playbook to display other facts based on your requirements.
In summary, Ansible facts serve as the informational backbone of automation, offering real-time insights into the systems being managed. By automating the gathering of key details about hosts, Ansible facts empower administrators to make informed decisions, tailor configurations, and create adaptive playbooks. This dynamic and versatile feature enhances the agility, efficiency, and intelligence of Ansible automation, streamlining the management of diverse IT environments with ease.
I hope that this blog has provided you with valuable insights into the practical and beneficial aspects of Ansible Facts.
💹Next day — Mastering Ansible Roles: Structuring, Reusability, and Best Practices.
⭐⭐⭐ Enjoy your learning….!!! ⭐⭐⭐