0%

0/1 Lessons

Course Introduction

• 19min

0 / 2 lessons complete

IT Lab Setup

• 31min

0 / 3 lessons complete

Installing Ansible

• 57min

0 / 4 lessons complete

Managing your Ansible Inventory

• 2hr 45min

0 / 8 lessons complete

Ansible Basics

• 2hr 54min

0 / 9 lessons complete

Ansible Roles

• 2hr 38min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 28min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 20min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 23min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Managing your Ansible Inventory

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, you will learn about managing your Ansible Inventory. By the end of this lesson, you should understand what an Ansible inventory is, how to define it, and the significance of organizing hosts and groups for efficient automation tasks.

This lesson will be completed on the Ansible Controller VM, so if you're not already logged into that VM, go ahead and do so now.

What is an Ansible Inventory?

An Ansible Inventory is a file (usually named inventory) that lists all the nodes or hosts that are reachable by Ansible. This file is used by Ansible to communicate with the machines you want to automate tasks on. A typical Ansible setup involves multiple machines, and the inventory file is where you define these machines and organize them.

Defining a Custom Inventory File

To start, let's create custom project directory named code, then we'll CD into the new directory:

# Create a custom project directory named code
mkdir ~/code

# CD into that directory
cd ~/code

Next we need to create a basic inventory file. This file can be in INI or YAML format. Here, we will use the INI format for simplicity. Create a file named inventory in your working directory with the touch command:

# Creates a file named inventory in your home directory
touch ~/code/inventory

You will need to CD into the ~/code directory from now on when you run Ansible because this is where we are saving our config

Next, add either the following INI formatted or YAML formatted configuration to define our inventory:

[webservers]
managed-node-1
managed-node-2

[controllers]
ansible-controller

In this example, there are two groups: webservers and controllers. Each group contains the respective hosts. The group names can be anything that makes sense for your setup. Here is the equivalent inventory in YAML format:

webservers:
  hosts:
    managed-node-1:
    managed-node-2:
controllers:
  hosts:
    ansible-controller:

Organizing Hosts and Groups

Organizing your hosts into groups is highly beneficial, especially when managing a large number of servers. Groups allow you to target a specific set of hosts for particular tasks. For instance, you might have tasks that should only run on web servers and others only on database servers.

You can also define variables for groups or individual hosts. These variables can be used to store specific data related to the hosts or groups, such as domain names, IP addresses, or any configuration specifics.

Running an Ansible Ping

Let's try using our inventory file to ping all hosts in the webservers group. First, make sure you have Ansible installed and your hosts are reachable. Then, run the following command:

ansible webservers -i inventory -m ping

This command uses the ping module to check the connection to all hosts in the webservers group. The -i option specifies the inventory file to use.

image 18
Ansible Adhoc Command on Inventory Group

We can see the pong response coming back to our Ansible Controller, and that means we have connectivity to the managed nodes!

We can run this ping module against all hosts as well by running the following command:

ansible all -i inventory -m ping

This will run the ping module against all hosts in our inventory file:

image 22
Ansible Adhoc Command Against All Hosts in our Inventory File

Default Inventory File Location in Ansible

In addition to defining custom inventory files, it's important to understand the concept of the default inventory file in Ansible. This knowledge is essential for efficient and organized Ansible management.

What is the Default Inventory File?

Ansible automatically looks for an inventory file at certain default locations. The most common default location is /etc/ansible/hosts. This file is used by Ansible if no other inventory is specified in the command line or in the Ansible configuration file.

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments