Inventory File Basics

Ansible needs to know what machines you’re going to be managing. We put this information in an inventory file so that Ansible has all the information it needs to connect and perform operations on our machines.

It doesn’t matter if you use a file or a directory of multiple files (as your machine count grows). It will all be figured out intelligently and is a great way to segment certain environments as we’ll see later.

Let’s start with the Basics

Create a new file, you can name it whatever you like, but traditionally it’s named “inventory”. Inside, put the following one line and save it.

localhost

What just happened? We listed one machine, the local computer you’re reading this on, and no other machines or additional information.

Recall from Your First Command that we had this inventory information inline, but now we moved the contents to a file, and pass the path instead of typing it out.

$ ansible all -i "localhost," -m debug -a "msg='Hello Ansible'"

Becomes

$ ansible all -i ./inventory -m debug -a "msg='Hello Ansible'"

So let’s go back to the inventory file. We have “localhost” in there now, but it could just as easily be our local machine’s IP address, usually “127.0.0.1” on Unix systems. Additionally, it could the be IP address of any machine you like, whether it’s you local computer, one on your local netowork (LAN), or a remote server on the Internet cloud.

Adding Connection Information

Chances are you can’t connect to an external machine (via SSH) using any old user name. Different machines run different operating systems and even if the operating systems are the same, they could be configured differently. There’s a lot of “different” that could go on. So, we’ll need to provide this information to ensure we can connect. Here’s a revised inventory file (the “#” indicate comments and are ignored).

# Defaults (no need to provide the "ansible_ssh" option)
192.168.0.10 ansible_ssh_port=22
# Customization for a local Ubuntu machine
192.168.0.11 ansible_ssh_user=ubuntu ansible_ssh_port=2222

While we could add password information here, it’s supposed to be secret. So, we’ll provide the password upon execution in order to not store it in plain-text in a file.