Skip to main content

Understand Core Components of Ansible – Inventories and Facts

Hands-On Lab

 

Photo of Rob Marti

Rob Marti

Linux Training Architect I in Content

Length

00:45:00

Difficulty

Beginner

The key to understanding Ansible lies with beginning to actually use Ansible. Grasping the basic concepts of inventories and facts is crucial to using Ansible, both in simple and more advanced implementations. This lab will make sure you have this basic understanding of where Ansible inventory files are, and how to set them up.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Understand Core Components of Ansible - Inventories and Facts

Introduction

The key to understanding Ansible lies with beginning to actually use Ansible. Grasping the basic concepts of inventories and facts is crucial to using Ansible, both in simple and more advanced implementations. This lab will make sure you have this basic understanding of where Ansible inventory files are, and how to set them up.

The Scenario

Our company hired a consultant to set up an Ansible proof of concept. Unfortunately the consultant didn't complete the setup and we've been tasked with doing it. While management deals with the consultant, we've discovered that Ansible has been installed but configured incorrectly. We need to fix the inventory so that ansible all -m ping works correctly, and then generate a list of facts about this system. We'll store these facts in /tmp/facts/.

Logging In

Use the credentials provided on the hands-on lab page to get into Server1 to begin with. Since we need root privileges, let's just run sudo -i right off and become root.

Fix the Ansible Inventory File

Running ansible all -m ping results in a failure, complaining about YAML parsing the inventory file. So we need to look at the Ansible inventory.

Edit /etc/ansible/hosts (with vim or whatever text editor is handy) and let's look at the file contents. It looks like this at the moment:

this is a comment
my.server.ip

We can see that the first line is supposed to be a comment, but it isn't commented out. The second line should be a server name or IP, but my.server.ip probably isn't either. It looks like an example, or a placeholder. So first, let's comment out the comment, and put an IP address in the second line. We're working on localhost, so we can use the equivalent IP address there. Now the file should look like this:

#this is a comment
127.0.0.1

Now run the first command again (ansible all -m ping) and after an authenticity prompt we should see the command complete successfully.

Generate a List of Facts About the Ansible Host

The easiest way to accomplish this task is with a one-liner:

ansible -m setup --tree /tmp/facts localhost

The ansible -m setup part grabs the facts, the --tree /tmp/facts specifies a directory for where to put the files containing these facts, and localhost is the server we're querying.

Now if we run ls /tmp/facts, we'll see 127.0.0.1 sitting there. This file contains all of the facts about the server at 127.0.0.1. If we'd run that command with all instead of localhost, then this directory would contain one file for each server in the inventory.

Conclusion

We were handed a broken Ansible setup, figured out what ailed it, and now we've got it up and running. And we've got a file now that contains all of the facts about our server. Congratulations!