Skip to content

The inventory.yaml File

Filename Location Group Project/Repository
inventory.yaml ./inventory.yaml configuration ansible

Why?

An inventory is an important part of the Ansible tool chain. Without an inventory Ansible will have no idea what systems we want to manage, where they can be found (within the network or remote network), how-to connect to them, and more.

Ansible inventories come in different shapes and sizes. You're very likely to find both static and dynamic inventories in the wild.

Our inventory is a static inventory. We know what systems we have in our network and it doesn't change. We can therefore write out the inventory ahead of time and use it to manage our (static) systems.

A dynamic inventory is used in networks that have an unknown or dynamic range of systems. A good example of this is an AutoScaling Group in AWS. The number of EC2 Instances inside of the group at any one time can be anything. Therefore we use tags on resources and a dynamic inventory can be used to find what instances exist at that point in time and then execute Ansible against them.

Breakdown

Our Ansible inventory is written in YAML. In the wild you might see other inventories written in INI format. That format is a bit easier to read until an inventory gets complex. INI files are also harder to generate from other (automated) sources versus YAML. That's why I've opted for a YAML based inventory file.

Note

Please ensure you're aware of how-to write a YAML file because as a format it's very particular about white space.

First we "create" a group called all:

1
all:

The all group technically already exists as it's a special group that basically means "everything". Still we're creating it for clarity.

Next we use the hosts keyword to define what hosts exists inside this group:

1
2
3
all:
  hosts:
    ssh-1.httpcats.net:

And then we define the hosts themselves, each with a variable being set called ssh_username:

1
2
3
4
5
6
all:
  hosts:
    ssh-1.httpcats.net:
      ssh_username: ubuntu
    ssh-2.httpcats.net:
      ssh_username: ubuntu

Note that the key used inside of the hosts dictionary is the hostname of the remote system itself. This is the internal DNS hostname used to resolve the remote host's IP and connect to it.

We're setting ssh_username so that Ansible knows what username to use. There are many other options available that you should explore in the official Ansible documentation.

The Solution

1
2
3
4
5
6
all:
  hosts:
    ssh-1.httpcats.net:
      ssh_username: ubuntu
    ssh-2.httpcats.net:
      ssh_username: ubuntu

Committing the Code

  1. Set your working directory to the configuration/ansible repository
  2. Save the file as inventory.yaml and use git add inventory.yaml to add it to the Git staging area
  3. Use git commit -am 'our primary inventory' to commit the file to our repository
  4. Push the code to GitLab.com: git push

Last update: August 25, 2021