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.


# Title Duration
1 How To Install Ubuntu Server in Oracle VirtualBox 10:21
2 How to Rename our Ubuntu Server 02:00
3 Installing Ansible on Ubuntu Server 00:33
4 Safety First, Safety Second - Snapshots are like Ctrl+Z 00:11
5 Managing the Ansible Inventory Hosts File 02:16
6 Ansible Ad Hoc Commands 04:27
7 Introduction to Ansible Playbooks 02:14
8 Ansible Handlers 01:39
9 Ansible Variables 03:16
10 Git Your Deploy Just Right 05:35
11 Ansible Roles 05:51
12 Looping in Ansible with_items 04:35
13 Ansible Files For Beginners 06:16
14 Variable Precedence - Where To Put Your Role Vars? 04:13
15 Ansible Templates 05:51
16 Ansible Inventory With Our Own Hosts Files 06:57
17 How to Manage Users with Ansible 08:32
18 Ansible Vault Tutorial 03:48
19 Ansible Galaxy Tutorial 10:03
20 Real World Ansible - Common Role Walkthrough 06:20
21 Ansible MySQL Tutorial 13:44
22 Ansible Symfony and nginx 09:37