Sunday, April 26, 2026

Ansible_Facts

 Facts


Ansible implements fact collecting through the a module called the setup module.
It collects detailed information (called facts) from managed nodes.
It collects detailed information (called facts) from managed nodes.
By default, Ansible automatically runs setup at the start of every play.


$ ansible ubuntu -m setup


- name: Gather facts manually
hosts: all
tasks:
- name: Run setup module
ansible.builtin.setup:
- debug:
msg: "Host {{ ansible_hostname }} has {{ ansible_memtotal_mb }} MB RAM"


Filtering Facts

Collect only specific facts
- name: Get only network-related facts
ansible.builtin.setup:
filter: ansible_default_ipv4

- name: Get only facts that start with 'ansible_processor'
ansible.builtin.setup:
filter: 'ansible_processor*'
- name: Get only facts that start with 'ansible_processor' and 'ansible_mem'
ansible.builtin.setup:
filter: 'ansible_processor*', 'ansible_mem*'

$ ansible all -m setup -a 'filter=ansible_all_ipv6_addresses'

Gather_Subset
Control what facts to collect
- name: Gather only minimal facts
ansible.builtin.setup:
gather_subset:
- min

Other options:
all (default)
hardware
network
virtual
ohai
facter


gather_subset: ! 'all,!hardware' # Gather all facts except hardware facts
gather_subset: ! 'all,!network' # Gather all facts except network facts
gather_subset: ! 'all,!virtual' # Gather all facts except virtual facts
gather_subset: ! 'all,!ohai' # Gather all facts except ohai facts
gather_subset: ! 'all,!facter' # Gather all facts except facter facts

Set timeout for fact gathering
- ansible.builtin.setup:
gather_timeout: 10

set_fact → create custom facts
Set_fact is a powerful module that allows you to create custom facts during playbook execution.

- name: Set a variable
set_fact:
my_var: "hello"

- name: Set multiple variables
set_fact:
env: "prod"
version: "1.2.3"

Append to a list variable
- name: Append to a list variable
set_fact:
my_list: "{{ my_list | default([]) + ['new_item'] }}"

Update a dictionary variable
- name: Update a dictionary variable
set_fact:
my_dict: "{{ my_dict | default({}) | combine({'key': 'value'}) }}"

Loop with set_fact
- name: Create a list of hostnames
set_fact:
hostnames: "{{ hostnames | default([]) + [item] }}"
loop: "{{ ansible_play_hosts }}"

- name: Build list dynamically
set_fact:
servers: "{{ servers | default([]) + [item] }}"
loop:
- web1
- web2


hostvars → access facts of other hosts
Hostvars is a powerful Ansible variable that allows you to access the facts and variables of other hosts in your inventory.
This can be particularly useful when you need to reference information about other hosts during playbook execution.

HOSTVARS VERSUS HOST_VARS
Please be warned that hostvars is computed when you run Ansible, while host_vars is a directory that you can use to define variables for a particular system.

What is hostvars?
👉 It is a dictionary of all hosts and their variables.

hostvars['hostname']['variable_name']


- name: Access hostvars
hosts: all
tasks:
- name: Show IP address of another host
debug:
msg: "The IP address of {{ item }} is {{ hostvars[item]['ansible_default_ipv4']['address'] }}"
loop: "{{ ansible_play_hosts }}"

Local facts
Local facts are custom facts that you can create on the managed nodes themselves.
They are stored in the /etc/ansible/facts.d/ directory on the managed nodes.
Local facts are useful for storing information that is specific to a particular host and may not be easily gathered through the setup module.
You can place one or more files on the remote host machine in the
/etc/ansible/facts.d directory. These files can be in JSON or INI format and must have a .json or .ini extension. When Ansible runs the setup module, it will automatically read these files and include the custom facts in the gathered facts for that host.

These facts are available as keys of a special variable named ansible_local.

To create a local fact, you can create a JSON or INI file in the /etc/ansible/facts.d/ directory on the managed node. For example, you could create a file called /etc/ansible/facts.d/custom_facts.json with the following content:

{
"custom_fact": "This is a custom fact"
}
Once you have created the local fact file, you can access the custom fact in your playbooks using the ansible_local variable. For example:
- name: Access local fact
hosts: all
tasks:
- name: Show custom fact
debug:
msg: "The custom fact is: {{ ansible_local.custom_facts.custom_fact }}"
Dynamic Inventory Script
An Ansible dynamic inventory script must support two command-line flags:
--host=<hostname> for showing host details
--list for listing groups


Magic Variables
Ansible provides several magic variables that are automatically available in your playbooks. These variables provide information about the playbook execution context, such as the current host, group, and task. Some commonly used magic variables include:

hostvars A dict whose keys are Ansible hostnames and values are dicts that map variable names to values for that host. This variable is useful for accessing variables of other hosts in the inventory.

inventory_host name The name of the current host as known in the Ansible inventory, might include domain name
inventory_hostname_short Name of the current host as known by Ansible, without the domain name(e.g., myhost)
group_names A list of all groups that the current host is a member of

- ansible_play_hosts: A list of all hosts in the current play
- ansible_play_batch: A list of hosts in the current batch (when using serial)
- ansible_play_name: The name of the current play
- ansible_play_role_names: A list of roles applied to the current play
- ansible_play_task_name: The name of the current task
- ansible_play_hosts_all: A list of all hosts in the inventory
- ansible_play_hosts_all_groups: A list of all groups in the inventory
- ansible_play_hosts_all_hosts: A list of all hosts in the inventory
- ansible_play_hosts_all_groups_hosts: A dictionary of all groups and their hosts in the inventory
- ansible_play_hosts_all_groups_hosts_vars: A dictionary of all groups, their hosts, and their variables in the inventory
- ansible_play_hosts_all_groups_hosts_vars_hostvars: A dictionary of all groups, their hosts, their variables, and the hostvars for each host in the inventory



Extra variable with the command-line option -e var=value
$ ansible-playbook playbook.yml -e "my_var=value"
$ ansible-playbook playbook.yml -e "my_var=value" -e "my_list=['item1', 'item2']"
$ ansible-playbook playbook.yml -e "my_dict={'key1': 'value1', 'key2': 'value2'}"


No comments:

Post a Comment