Lesson 4.3: Managing Facts
What Are Ansible Facts?
Ansible facts are system properties and variables automatically collected from managed hosts during playbook execution. These facts provide crucial information about the system’s hardware, network, OS, and other configurations, allowing playbooks to adapt dynamically.
Facts are gathered using the setup module, which runs by default at the beginning of playbook execution unless explicitly disabled.
Using Facts in Playbooks
Facts can be referenced in playbooks using Jinja2 templating syntax :
- name: Display system information hosts: all tasks: - name: Show OS family debug: msg: "This system runs on {{ ansible_os_family }} OS."
Fact Variable | Description |
---|---|
ansible_facts['fqdn'] | Fully Qualified Domain Name (FQDN) |
ansible_facts['hostname'] | Hostname of the system |
ansible_facts['default_ipv4']['address'] | Primary IPv4 address |
ansible_facts['os_family'] | OS family (e.g., RedHat, Debian) |
ansible_facts['distribution'] | Linux distribution (e.g., Ubuntu, CentOS) |
ansible_facts['memtotal_mb'] | Total memory in MB |
ansible_facts['processor_count'] | Number of CPU cores |
Facts Used in This Example
In the given example, Ansible facts are used to dynamically generate a /etc/myhosts file for all managed nodes. The key facts used are:
ansible_facts['default_ipv4']['address']
- Retrieves the primary IPv4 address of each host.
- Example: 192.168.208.150 for node1.
ansible_facts['fqdn']
- Returns the Fully Qualified Domain Name (FQDN) of the host.
- Example: node1.example.com (if set).
ansible_facts['hostname']
- Provides the short hostname of the machine.
- Example: node1.
[devops@ansible-server ansible]$ cat hosts.j2 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 {% for x in groups['all'] %} {{ hostvars[x]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[x]['ansible_facts']['fqdn'] }} {{ hostvars[x]['ansible_facts']['hostname'] }} {% endfor %} [devops@ansible-server ansible]$ [devops@ansible-server ansible]$ vim gen_hosts.yml [devops@ansible-server ansible]$ cat gen_hosts.yml - name: Playbook to create hosts file hosts: all tasks: - name: Template a file to /etc/hosts ansible.builtin.template: src: /home/devops/ansible/hosts.j2 dest: /etc/myhosts [devops@ansible-server ansible]$ ansible-playbook --syntax-check gen_hosts.yml playbook: gen_hosts.yml [devops@ansible-server ansible]$ ansible-playbook gen_hosts.yml
How These Facts Are Used in the Template (hosts.j2): The Jinja2 template iterates over all hosts in the inventory using:
{% for x in groups['all'] %} {{ hostvars[x]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[x]['ansible_facts']['fqdn'] }} {{ hostvars[x]['ansible_facts']['hostname'] }} {% endfor %} This generates a /etc/myhosts file with entries like:
192.168.208.150 node1.example.com node1 192.168.208.151 node2.example.com node2 192.168.208.152 node3.example.com node3 192.168.208.153 node4.example.com node4
Each line dynamically maps the IP, FQDN, and hostname of every managed node using Ansible facts.
Automating System Information Collection
In this example, Ansible facts are used to generate a hardware report for each managed node, storing details in /root/hw_report.txt. The key facts used are:
ansible_facts['hostname']
- Retrieves the short hostname of the machine.
- Example: node1.
ansible_facts['memtotal_mb']
- Returns the total amount of system memory (RAM) in megabytes.
- Example: 3585 MB.
ansible_facts['bios_version']
- Provides the BIOS version of the machine.
- Example: VMW201.00V.21805430.BA64.2305221830.
How These Facts Are Used in the Template (hwreport.j2):
Inventory Hostname {{ ansible_facts['hostname'] }} Total Memory in MB {{ ansible_facts['memtotal_mb'] }} BIOS Version {{ ansible_facts['bios_version'] }} This generates a report for each node, such as:
Inventory Hostname node1 Total Memory in MB 3585 BIOS Version VMW201.00V.21805430.BA64.2305221830
Each node gets its own report with dynamically collected system information using Ansible facts.
[devops@ansible-server ansible]$ vim hwreport.j2 [devops@ansible-server ansible]$ cat hwreport.j2 Inventory Hostname {{ ansible_facts['hostname'] }} Total Memory in MB {{ ansible_facts['memtotal_mb'] }} BIOS Version {{ ansible_facts['bios_version'] }} [devops@ansible-server ansible]$ cat hwreport.yml - name: Playbook to create device config file hosts: all tasks: - name: Template a file to /roor/hw_report.txt ansible.builtin.template: src: /home/devops/ansible/hwreport.j2 dest: /root/hw_report.txt [devops@ansible-server ansible]$ ansible-playbook hwreport.yml [devops@ansible-server ansible]$ ansible all -m command -a 'cat /root/hw_report.txt' node1 | CHANGED | rc=0 >> Inventory Hostname node1 Total Memory in MB 3585 BIOS Version VMW201.00V.21805430.BA64.2305221830 node3 | CHANGED | rc=0 >> Inventory Hostname node3 Total Memory in MB 3585 BIOS Version VMW201.00V.21805430.BA64.2305221830 node2 | CHANGED | rc=0 >> Inventory Hostname node2 Total Memory in MB 3582 BIOS Version VMW201.00V.21805430.BA64.2305221830 node4 | CHANGED | rc=0 >> Inventory Hostname node4 Total Memory in MB 3585 BIOS Version VMW201.00V.21805430.BA64.2305221830