Updated April 5, 2023
Introduction to Ansible Facts
Ansible facts are the information of remote hosts which is gathered by the Ansible controller. This information is stored in predefined variables on the controller node and the whole set of this information is prepared in JSON format. This is a very important feature as we can make decisions about which task is to perform on which remote machine based on these facts.
Also, when you are playing with real-time data, this is useful to fetch these facts and use them in playbooks as variables. This information can be the hostname, IP address, mac address, installed OS related information, the current status of the machine, etc.
Explaining Ansible Facts
Ansible facts are gathered using the setup module, which runs in the background every time. But this can be switched off by using mentioning gather_facts: no in the playbook. We can run setup module and register the output in a variable, the whole set of information will be on JSON format, if you need a piece of information from this set, you can take that apart and use this piece only.
The set of facts which is gathered from remote hosts can be divided into three categories:
- List: This is the list of items. This can be a list of interfaces etc. The information inside lists is placed in square brackets [].
- Dictionary: This is the collection of key pair values, the information inside the dictionary is placed in curly brackets {}.
- Ansible unsafe text: Which is a variable, holding value and there is no subpart of
A small snippet of the gathered facts is below. Here you can see, information of remote-host is gathered and placed in variables like in ansible_all_ipv4_addresses variable which will get all our IPv4 addresses.
Also, note that some facts contain values in [] like ansible_all_ipv4_addresses which means it is a List, some variables like ansible_apparmor have information in {} which means it is a dictionary and some are simply putting values after the colon (:) like ansible_architecture which means it is ansible unsafe text category.
ansible host-one –m setup
To get the category of a fact use a filter type_debug, which will tell whether your variable/fact is a List, a Dictionary, or an Ansible unsafe text. To check this, create a playbook like below:
---
- hosts:
all
tasks:
- set_fact:
check_category: "{{ ansible_hostname|type_debug }}"
- debug:
msg: 'Variable "ansible_hostname" with value "{{ ansible_hostname }}" have category "{{ check_category }}"'
Then execute it, you will get the output like below, where you can see the category of fact ansible_hostname
ansible-playbook ansible_fact_category_check.yaml
How Facts Done in Ansible?
Ansible facts are fetched from remote hosts using setup module which runs automatically every time, if not disabled. This can be done by running the setup module on the command line using the adhoc method or by default in a playbook.
Ad hoc method
Run below command on the Ansible controller node to get all available facts.
ansible host-one -m setup
Also, the same is possible by running below the adhoc command.
ansible all -m gather_facts --tree /tmp/facts
Also, if you need to show only a specific fact, then you can use like below:
ansible host-two -m setup -a "filter=ansible_mounts"
The output will be long in the JSON format. Like below.
Playbook method
If you have a playbook like below where we have not mentioned the setup module.
---
- hosts:
all
tasks:
- debug:
var: ansible_architecture
After running it. In the output, you will see that we get the variable ansible_architecture value.
ansible-playbook ansible_fact.yaml
But if you mention gather_facts: no like below.
---
- hosts: all
gather_facts:
no tasks:
debug:
var: ansible_architecture
Then you will see, in output, the variable ansible_architecture will be shown as not defined.
ansible-playbook ansible_fact.yaml
Examples of Ansible Facts
Now by using examples, we will try to learn about ansible facts, which you might have to use in day to day operations. We will take some examples, but before going there, we first understand our lab used for testing purpose.
Here we have an Ansible control server named ansible-controller and two remotes hosts named host- one and host-two. We will create playbooks and run Ansible commands on the ansible-controller node and manage the users on remote hosts. Below are some ad hoc commands which are easy to use but not reusable like playbooks.
To get a fact by using filter and setup:
ansible all -m setup -a "filter=ansible_distribution"
The output will be like below:
ansible all -m setup -a "filter=ansible_distribution"
To get all facts related to a keyword like a processor, memory, product, etc. We can make use of wildcard characters like below: –
ansible all -m setup -a "filter=ansible_processor*"
The output will be like below:
You can use gather_subset, to get only facts related to a subject. Subset’s name can be all, min, hardware, network, virtual, ohai, and factor. If exclamation sign (!) is put as initial to a subset, Then, that subset’s facts will not be fetched. Like in the below example, where we will fetch only virtual subset related facts and by using the !all, !min, we are not allowing even default minimum facts.
ansible host-one -m setup -a 'gather_subset=virtual,!all,!min'
The output will be like below:
In the below example we will use a condition, in which we will check a fact-value on remote hosts, and then based on that value our condition will allow execution of tasks.
Here, we want to create a file only on host-one, so we check using ansible facts that if ansible_hostname has a value equal to host-one. If that is true then, the file will be created and for other remote hosts, the operation will be skipped.
---
hosts:
all
tasks:
name: When hostname is host-one, create a file named example.ini under /tmp file:
path:
/tmp/example.ini
state: touch
when: ansible_hostname == "host-one"
name: This is under debug to ensure it was checked on all remote hosts debug:
msg: Hostname is "{{ ansible_hostname }}"
The output will be like below:
ansible-playbook ansible_fact_condition.yaml
Conclusion
Ansible fact is a useful tool. Which will run and helps you to gather information from your remote hosts? But you may acknowledge that if you have a lot of hosts to be managed by Ansible, then gathering facts and not using these facts will only add burden to your bandwidth and processing capacity. So better analysis first that whether you need facts or not and then use these accordingly.
Recommended Articles
This is a guide to Ansible Facts. Here we discuss How Facts Done in Ansible with explanation and along with the respective examples for better understanding. You may also look at the following articles to learn more –