Level 1
0 / 100 XP

Managing your Ansible Inventory

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:

Text
# 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:

Bash
# 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:

JSON
[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:…