Difficulty: beginner
Estimated Time: 10 minutes

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.

In this Lab we will be exploring Inventory Files in Ansible. Lab exercises are meant to help you develop inventory files for various use cases.

Ansible Inventory Files

Step 1 of 8

Creating an Inventory

By default, Ansible will read /etc/ansible/hosts as its default inventory file. But 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

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 use your terminal to verify that myhosts has been created: cat myhosts

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