Using Ansible Facts to View System Properties
Jun 08, 2023 • 0 Minute Read
Using Ansible for system automation provides a number of powerful features, one being Ansible facts. In this blog, we'll go over Ansible facts and how you can use a few parameters with the
ansible
command to explore which facts are available to Ansible during an execution.
What are Ansible Facts Anyway?
Simply put, Ansible facts are system properties that are collected by Ansible when it executes on a remote system. The facts contain useful details such as storage and network configuration about a target system. They may be outputted to a file as a type of system report, or they may be used during Ansible playbook execution to make runtime decisions.How Do We Use Facts?
The great thing about facts is that —assuming you have Ansible installed and configured at a basic level— you can use theansible
command with the built-in setup module to take a look at a large number of facts with relative ease! Let’s take a look:As you can see, we use basic Ansible ad-hoc
syntax providing the setup module using the -m
flag. A large number of facts are output by default. The number of facts provided is pretty impressive but are not terribly useful on their own. We can slim down the results by using the filter parameter:We use the -a
flag to provide the filter parameter which takes a simple pattern-match expression, such as an asterisk, as a wild card. Using patterns lets us search through the facts for specific facts that we want to know about.
Facts in Playbooks
A great thing about facts is that they can be put to work within an Ansible playbook! This means you may use system runtime properties within your configurations! By default, facts are gathered every time a playbook runs. You may toggle whether or not facts are collected on playbook execution using thegather_facts
parameter. The only reason you might consider disabling fact collection is for older systems since there is a small performance cost associated with gathering facts. The trade-off for disabling facts, of course, is that you will not be able to use them within your playbook.