System Fact Variables in Ansible
Charanjit Singh Cheema
Cloud Architect | Expert in Linux Systems, Ansible, Terraform Automation, and Cloud Solutions | Proven Leadership in Global IT Projects
System fact variables in Ansible are automatically discovered pieces of information about the remote systems that Ansible manages. These facts provide a comprehensive overview of the system's state, including hardware, network interfaces, operating system details, and more. Ansible gathers these facts at the beginning of a playbook run, allowing users to make decisions and tailor their automation processes based on the current state of the managed nodes.
Understanding System Fact Variables
System fact variables are gathered by Ansible using the setup module. This module collects a wide array of information from the managed nodes and makes it available as variables within your playbooks. These facts can include details such as:
- Hardware Information: CPU architecture, memory, disk space.
- Network Information: IP addresses, network interfaces, MAC addresses.
- Operating System Information: Distribution name, version, kernel details.
- User Information: Currently logged-in users, user groups.
Setting Facts in Ansible
While system fact variables provide useful information about the remote systems, there are times when you need to define your custom facts to pass information between tasks or roles. This is done using the set_fact module in Ansible. Custom facts can be useful for storing results of operations, making decisions based on dynamic data, and more.
Benefits of Using System Facts and Custom Facts
1. Dynamic Decision Making: Using system facts, playbooks can adapt to the state of the managed nodes, making them more versatile and powerful.
2. Simplification of Complex Playbooks: By using custom facts, you can break down complex operations into smaller, more manageable tasks.
3. Improved Reusability: Facts allow for the creation of reusable roles and tasks, as they can adapt based on the environment they are deployed in.
4. Enhanced Reporting: Gathering system facts provides detailed information about the infrastructure, which can be used for reporting and auditing purposes.
领英推荐
Example - Using System Facts and Custom Facts
Let’s take this scenario assume we need to configure a web server by using ansible playbook. Before proceeding, we want to ensure that the server has enough memory and disk space. We'll use system facts to gather this information and set custom facts to store our results and make decisions.
Ansible Playbook
---
- name: Configure Web Server
hosts: webservers
gather_facts: yes
tasks:
- name: Check available memory
set_fact:
sufficient_memory: "{{ ansible_memtotal_mb > 2048 }}"
- name: Check available disk space
set_fact:
sufficient_disk: "{{ ansible_devices['sda']['size'] > 20 }}"
- name: Ensure sufficient resources are available
fail:
msg: "Insufficient resources: Memory or Disk space"
when: not (sufficient_memory and sufficient_disk)
- name: Install Apache Web Server
yum:
name: httpd
state: present
when: sufficient_memory and sufficient_disk
- name: Start Apache Service
service:
name: httpd
state: started
when: sufficient_memory and sufficient_disk
- name: Create index.html
copy:
content: "Welcome to the web server"
dest: /var/www/html/index.html
when: sufficient_memory and sufficient_disk
Explanation
1. Gather Facts: The playbook starts by gathering system facts using the gather_facts: yes directive.
2. Check Available Memory: A custom fact sufficient_memory is set to true if the total memory in megabytes is greater than 2048 MB.
3. Check Available Disk Space: Another custom fact sufficient_disk is set to true if the size of the disk sda is greater than 20 GB.
4. Ensure Sufficient Resources: If either memory or disk space is insufficient, the playbook fails with a message.
5. Install Apache: Apache web server is installed only if both memory and disk space are sufficient.
6. Start Apache Service: Apache service is started under the same condition.
7. Create index.html: An index.html file is created with a welcome message if the resources are sufficient.
Wrap up!
System fact variables in Ansible provide a powerful way to dynamically interact with the remote systems based on their current state. By leveraging these facts and setting custom facts, you can create more adaptable and robust playbooks. This flexibility allows for dynamic decision-making, simplification of complex automation tasks, and improved reusability and reporting. Whether you are managing a few servers or an entire data center, understanding and using system facts effectively can significantly enhance your automation processes.