Ansible Facts are used to fetch information about the managed nodes. The fact can provide a wide range of information about nodes/servers/systems etc.,
- Processor details
- Memory
- Python version
- OS release, distribution, etc.
Ansible’s setup module is used to gather these facts/information.
Facts are gathered in both Ansible Ad-Hoc commands and in the Ansible playbooks. In Ad-hoc commands, we need to explicitly run the setup module command, whereas in playbooks facts are by default gathered.
Syntax
ansible localhost -m setup
The setup module default will display all information about your node/system.
The facts information will be displayed in JSON/python dictionary format.
We may not be interested in such large Node/system information. We may be interested only in specific information about the node. Ansible does provide us the option to fetch only the information which is required from the facts.
We can retrieve specific required data from facts by passing the argument filter in your setup module ad-hoc command.
Syntax
ansible localhost -m setup -a "filter=ansible_python_version"
ansible localhost -m setup -a "filter=ansible_distribution"
Types of Ansible Facts
- Default Facts, the default system information fetched using the setup module are called default facts. These facts do not provide details of any third-party application installed on the node, like MySQL, Splunk, etc.
- Custom Facts are created to gather information about the third-party applications/services installed on your node/system, like Splunk, Mysql server, etc.
Custom Facts are required when we need information about third-party applications installed on the host or nodes. This information is like version details of third-party applications Splunk, Apache, MySQL server, etc.
Steps to create Custom facts
- Create your custom facts file at /etc/ansible/facts.d directory
- Create custom facts file with exention .fact under your /etc/ansible/fact.d directory.
- Custom facts files can be created using python, and shell script, but the files should be saved with .fact as a file extension.
- Set executable permission to the fact files.
Syntax to execute custom facts
ansible localhost -m setup -a "filter=ansible_local"