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 the
ansible 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 the
gather_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.
Features such as Ansible facts are just one of many tools at your disposal in the Ansible toolbox. When paired with functionality such as conditionals or templates, facts can take your system automation to the next level. Combining these features allows for dynamic configuration on a per system basis. This is only a small demonstration of some of the power Ansible delivers!
Ansible provides simple yet powerful automation no matter your operating system or cloud provider. If you are looking for a quick introduction to Ansible and have a basic grasp on the Linux operating system, you should check out Linux Academy’s recently refreshed Ansible Quick Start Course! We cover basic installation, how to write simple playbooks, and a number of other essential features for the Professional who is looking for a short introduction to Ansible!