Working with Ansible facts
Ansible is not only used to deploy and configure remote hosts. It can be used to gather all kinds of information and facts about them. The facts collection can take significant amount of time to collect everything from a busy system, but will provide a full view of the target machine.
The facts that are gathered can be used inside the playbook later, to design a task condition. For example, we used the when
clause to limit the openssh
installation to only CentOS-based systems:
when: Ansible_distribution == "CentOS"
You can enable/disable fact gathering in the Ansible plays by configuring gather_facts
on the same level as hosts and tasks:
- hosts: centos-servers gather_facts: yes tasks: <your tasks go here>
Another way to gather facts and print them in Ansible is to use the setup
module in the ad hoc mode. The returned results are in the form of nested dictionaries and lists, to describe the remote target facts, such as the server architecture, memory,...