Managing the Ansible Inventory Hosts File

In this video we look at the Hosts file that Ansible creates for us as part of the install process.

This file uses a INI (.ini) formatting style.

By default, this file is populated with example data, which whilst great as an initial reference, is not what we want to use moving forwards.

As such, we will start by copying the Hosts file to a back up copy, then creating our own Hosts file to continue learning with:

cd /etc/ansible
sudo mv hosts hosts.bak
sudo touch hosts

Then we will begin by creating our own Hosts file entry. I am going to use vi as my editor, but feel free to use whichever editor you prefer, vi certainly isn't the easiest:

sudo vi hosts

Inside the file, we will add in our first entry:


In this example [local] represents the group we want to group our hosts under.

We then add in to the local group.

Another way to think of this is as though a PHP array:

$localGroup = [''];


$localGroup = array('');

if moving with the times is not something you're fond of ;)

Of course, Ansible has nothing to do with PHP, that's purely for explanatory purposes.

The analogy follows through in that you can have multiple groups like you would have multiple variables, and multiple entries in your group as you may have multiple entries in an array.

In more advanced scenarios, you can also have groups that reference other groups, child groups, host ranges, and so on. You will see some of these options if you look inside the hosts.bak file we created earlier.

sudo vi hosts.bak

This barely scratches the surface of what you can do with your Hosts file, and if you're keen to know more, then check out the documentation page which is full of useful examples.

However, for what we are going to do in the short term, this is more than enough info to get started, so ... let's get started!

Code For This Course

Get the code for this course.