Lab 005 - Facts¶
- In this section, we will cover Ansible Facts.
- Ansible facts are essentially “Ansible Scripts” and constitute one of the building blocks of Ansible.
- Ansible facts are data corresponding to your remote systems, which includes operating systems, IP addresses, attached filesystems, and more.
- Ansible facts are gathered, and relate to target nodes (host nodes to be configured). They are returned back to the controller node.
01. How to View Facts?¶
- Ansible gathers facts about remote systems using the
setupmodule. -
You can view facts of a remote machine by running the following command:
-
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" } } }
02. How to use facts in playbooks?¶
- Facts allow you to base your playbook logic on the properties of the target hosts.
- All facts are prefixed with
ansible_x. - For example, to access the operating system distribution of a host, you would use
ansible_distribution. - Here are some examples of how to use facts in playbooks:
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
03. 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) |
ansible_hostname | Hostname of the machine |
ansible_os_family | OS family (Debian, RedHat, Windows) |
ansible_facts | All gathered facts |
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 |
User-defined Facts¶
| Fact | Description |
|---|---|
ansible_user | Current user |
ansible_group | Current group |
04. Disabling fact gathering¶
- By default, Ansible gathers facts before running a playbook.
- In order to disable it, add the following at the beginning of your playbook:
- To disable fact gathering, set
gather_factstonoin your playbook:
- hosts: all
gather_facts: no
tasks:
- name: Print a message
debug:
msg: "Facts gathering is disabled!"
05. Custom Facts¶
- You can define custom facts by creating
.factfiles, placing them inside/etc/ansible/facts.d/directory on the managed host.
Example: Creating a custom fact¶
-
Create the file
/etc/ansible/facts.d/custom.factwith: -
Retrieve the fact in a playbook:
Using Custom Facts¶
- Another way to define your own custom facts using the
set_factmodule in your playbooks. -
Here is an example:
06. Hands-on¶
-
Use the
setupmodule to gather and print all facts fromlinux-server-1.Solution
-
Use the
setupmodule to gather and print only the network-related facts fromlinux-server-2.Solution
-
Create a playbook that installs
nginxonly if the target host is runningUbuntu.Solution
-
Create a playbook that restarts a service only if the host has less than
4GBof RAM.Solution
-
Create a custom fact that defines the environment as
developmentand print it in a playbook.Solution
First, create
/etc/ansible/facts.d/custom.facton the host:Then playbook:
-
Create a playbook that sets a custom fact
deployment_stagetostagingand prints it.Solution
-
Disable fact gathering in a playbook and print a message indicating that facts are not gathered.
Solution
-
Create a playbook that prints the hostname of each target host using the appropriate fact.
Solution
-
Create a playbook that checks the OS family and installs a package accordingly (e.g.,
nginxforDebian,httpdforRedHat).Solution
-
Create a playbook that gathers facts and prints the total memory of each host.
Solution
-
Create a playbook that checks the default gateway and prints it.
Solution
-
Create a playbook that sets a custom fact
backup_requiredtoyesand prints a message if backup is required.Solution
-
Create a playbook that uses the
ansible_userfact to print the current user on each host.Solution
-
Print the IP addresses of all the machines.
Solution
-
Bonus - Try printing the address of
linux-server-2only, without modifying the inventory file.Solution
07. Summary¶
🔹 Ansible facts provide system details dynamically.
🔹 They are automatically gathered using the setup module.
🔹 They are useful for conditional logic in playbooks.
🔹 Facts may include OS, networking, CPU, memory and more.
🔹 Custom facts can be created for customized automation.