Difficulty: Intermediate
Estimated Time: 15-20 minutes

Introduction

In configuration management, the tool that you’re using needs to know which machines it should run on. This is known as an inventory. Without an inventory, you would have a set of playbooks that define your desired system state, but you wouldn’t know which machines you needed to run them against.

With Puppet and Chef, this information is stored on a central server. As there is no central server in Ansible, we will need another way to get all of this information to the code that runs to enforce the desired state. This is where the inventory file comes in.

Congratulations!

If the playbook tells Ansible what to run, inventory files tell them where to run it.

In summary, the same playbook should always be able to run on a host and - assuming there are no errors - produce the desired state, regardless of what state the host started in. For example, should your playbook halt halfway through due to a network error, it would still finish successfully when you re-run it.

Next: Ansible-Installing Wordpress

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Ansible-Inventory

Step 1 of 5

Creating an Inventory

By default, Ansible will read /etc/ansible/hosts as its default inventory file. Using this file is not recommended though. You should maintain a different inventory file for each project that you have and pass it to both the ansible and ansible-playbook commands using the –i option.

Here is an example of passing a custom inventory file to ansible before running the ping module:

ansible all –i /path/to/inventory –m ping

You can run that command yourself, but first you gotta create an inventory file. In this tutorial you have access to one host named host01. To let Ansible know about it, put its hostname in an inventory file.

1. We'll call our inventory file myhosts, and you'll add one group called group1:

echo "[group1]" > myhosts

2. Then, add the host to the group, while also passing the username to use for SSH access, as an inventory parameter.

echo "host01 ansible_ssh_user=ubuntu" >> myhosts

3. You can obviously look at the editor, but of course, you can also use your terminal to verify that myhosts has been modified correctly, also:

cat myhosts

So, now we can move on to running that command!