 [](https://www.linkedin.com/in/nirgeier/) [](mailto:nirgeier@gmail.com) [](mailto:nirg@codewizard.co.il)
this project Lab 005 - Facts¶
- In this section, we will cover Ansible Facts
- Ansible facts are “Ansible Scripts” and are one of the building blocks of Ansible.
- Ansible facts are data related to your remote systems, including operating systems, IP addresses, attached filesystems, and more.
- Ansible facts are gathered about target nodes (host nodes to be configured) and returned back to controller nodes.
How to View Facts?¶
You can view facts of a remote machine by running:
🔹 Example Output (Truncated for brevity):
{
"ansible_facts": {
"ansible_distribution": "Ubuntu",
"ansible_distribution_version": "22.04",
"ansible_architecture": "x86_64",
"ansible_memory_mb": {
"real": {
"total": 7989,
"used": 2034
}
},
"ansible_default_ipv4": {
"address": "192.168.1.10",
"netmask": "255.255.255.0",
"gateway": "192.168.1.1"
}
}
}
How to Use Facts in Playbooks?¶
- Facts allows you to base your playbook logic on the properties of the target hosts.
- all facts are prefixed with
ansible_x.
Example: Installing Packages Based on OS¶
---
- hosts: all
tasks:
- name: Install Nginx on Debian using APT
ansible.builtin.apt:
name: nginx
state: present
when: ansible_distribution == "Ubuntu"
- name: Install Nginx on RedHat using DNF
ansible.builtin.dnf:
name: nginx
state: present
when: ansible_distribution == "CentOS"
Example: Conditional Execution Based on Memory¶
- name: Restart Service if Memory is Low
ansible.builtin.service:
name: my_service
state: restarted
when: ansible_memory_mb.real.total < 4000
Commonly Used Facts¶
System Information¶
| Fact | Description |
|---|---|
ansible_distribution | OS name (Ubuntu, CentOS, Windows) |
ansible_distribution_version | OS version (22.04, 9.1, 10) |
ansible_architecture | System architecture (x86_64, arm) |
Networking¶
| Fact | Description |
|---|---|
ansible_default_ipv4.address | Default IP address |
ansible_default_ipv4.gateway | Default gateway |
ansible_fqdn | Fully Qualified Domain Name |
ansible_dns.nameservers | DNS servers |
Hardware¶
| Fact | Description |
|---|---|
ansible_memory_mb.real.total | Total RAM in MB |
ansible_processor_count | Number of CPUs |
ansible_processor_cores | Number of CPU cores |
Disabling Fact Gathering¶
By default, Ansible gathers facts before running a playbook. To disable it:
- hosts: all
gather_facts: no
tasks:
- name: Print a message
debug:
msg: "Facts gathering is disabled!"
Custom Facts¶
You can define custom facts by creating .fact files in /etc/ansible/facts.d/ on the managed host.
Example: Creating a Custom Fact¶
1️⃣ Create a file /etc/ansible/facts.d/custom.fact with:
2️⃣ Retrieve the fact in a playbook:
Summary¶
🔹 Ansible facts provide system details dynamically.
🔹 They are automatically gathered using the setup module.
🔹 Useful for conditional logic in playbooks.
🔹 Facts include OS, networking, CPU, memory, and more.
🔹 Custom facts can be created for customized automation.
- Print the IP addresses of all the machines
- Bonus - Try print the addresss of
linux-server-2only without modifying the inventory file.
:arrow_backward: /Labs/004-playbooks Back to labs list /Labs/006-git :arrow_forward: