Complete Guide to Ansible Predefined Variables: How to Access and Use System Facts

Nov 23, 2025 · Programming · 7 views · 7.8

Keywords: Ansible | Predefined Variables | Fact Gathering | Setup Module | Automation Configuration

Abstract: This article provides a comprehensive guide to accessing and using predefined variables in Ansible. By analyzing Ansible's fact gathering mechanism, it explains how to use the setup module to obtain complete system information variable lists. The article includes detailed code examples and actual output analysis to help readers understand the structure of ansible_facts and common variable types. It also compares the advantages and disadvantages of different variable retrieval methods, offering comprehensive variable management guidance for Ansible users.

Overview of Ansible Fact Variables

Ansible, as a powerful automation configuration management tool, provides a rich system of predefined variables known as "facts". Facts are system information automatically collected by Ansible when connecting to target hosts, including network configuration, hardware information, operating system details, and more. These variables can be directly used in playbooks and template files, greatly simplifying the writing of automation scripts.

Methods for Obtaining Complete Variable Lists

To view all available fact variables for a specific host, the most direct method is using Ansible's setup module. This module collects and returns the complete system information of the target host.

ansible -m setup hostname

In the above command, hostname should be replaced with the actual target hostname or IP address. After executing this command, Ansible will output a structured JSON object containing all fact variables for that host.

Analysis of Fact Variable Structure

The data returned by the setup module is a nested dictionary structure, primarily containing the following key sections:

{
    "ansible_facts": {
        "ansible_all_ipv4_addresses": ["10.0.2.15", "192.168.10.10"],
        "ansible_architecture": "i386",
        "ansible_distribution": "Ubuntu",
        "ansible_default_ipv4": {
            "address": "10.0.2.15",
            "alias": "eth0",
            "gateway": "10.0.2.2"
        }
    },
    "changed": false
}

From the output structure, we can see that fact variables are mainly categorized into several types: network information (IP addresses, interface configuration), system architecture, operating system information, hardware information, etc. Each variable is prefixed with ansible_ for easy identification.

Common Fact Variable Types

Based on actual output analysis, Ansible fact variables primarily include the following types:

Network-Related Variables

Network variables provide the host's network configuration information:

"ansible_eth0": {
    "active": true,
    "device": "eth0",
    "ipv4": {
        "address": "10.0.2.15",
        "netmask": "255.255.255.0",
        "network": "10.0.2.0"
    },
    "macaddress": "08:00:27:12:96:98"
}

These variables are very useful when configuring network services, allowing dynamic retrieval of interface IP addresses and network configurations.

System Information Variables

System information variables contain operating system and kernel details:

"ansible_distribution": "Ubuntu",
"ansible_distribution_version": "12.04",
"ansible_kernel": "3.2.0-23-generic-pae",
"ansible_os_family": "Debian"

These variables allow playbooks to execute corresponding configuration tasks based on different operating system types.

Hardware Information Variables

Hardware variables provide CPU, memory, and storage device information:

"ansible_processor": ["Pentium(R) Dual-Core CPU E5300 @ 2.60GHz"],
"ansible_memtotal_mb": 369,
"ansible_devices": {
    "sda": {
        "model": "VBOX HARDDISK",
        "size": "80.00 GB"
    }
}

Using Fact Variables in Playbooks

Fact variables can be directly referenced in Ansible playbooks:

- name: Configure network service
  hosts: webservers
  tasks:
    - name: Display host IP address
      debug:
        msg: "Host {{ inventory_hostname }} IP address is {{ ansible_eth0.ipv4.address }}"
    
    - name: Check available memory
      debug:
        msg: "System total memory: {{ ansible_memtotal_mb }} MB"

In Jinja2 templates, fact variables are equally available:

Server Configuration Information:
Hostname: {{ ansible_hostname }}
Operating System: {{ ansible_distribution }} {{ ansible_distribution_version }}
Kernel Version: {{ ansible_kernel }}

Advanced Variable Retrieval Techniques

Beyond the basic setup module, template methods can be used to obtain more complete variable information, including inventory variables and group variables:

- hosts: all
  tasks:
    - template:
        src: templates/dump_variables
        dest: /tmp/ansible_variables
    - fetch:
        src: /tmp/ansible_variables
        dest: "{{ inventory_hostname }}_ansible_variables"

Corresponding template file content:

HOSTVARS (ANSIBLE GATHERED, group_vars, host_vars):
{{ hostvars[inventory_hostname] | to_yaml }}

PLAYBOOK VARS:
{{ vars | to_yaml }}

Considerations and Best Practices

When using Ansible fact variables, the following points should be noted:

1. Fact gathering may impact performance, especially when managing large numbers of hosts. This can be disabled by setting gather_facts: false in the playbook.

2. Some variables may have different names or structures on different systems. It's recommended to add appropriate error handling in playbooks.

3. For network interface variables, interface names (such as eth0, ens33) may vary by system. It's recommended to use universal variables like ansible_default_ipv4.

4. Fact variables are read-only and cannot be modified during playbook execution.

Conclusion

Ansible's fact variable system provides powerful infrastructure for automated configuration management. By properly utilizing these predefined variables, more flexible and portable playbooks can be written. Mastering the methods for accessing and using fact variables is an important step toward becoming an advanced Ansible user. It's recommended to practice extensively in real projects to become familiar with how variables change in different environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.