Foundations / Lesson 2 of 3

Inventory

Tell Ansible which machines exist, how they are grouped and what settings they need.

6 min Beginner 7 steps
webserversdbserversCloud APIControl nodeweb1.example.comweb2.example.cominventory.inidb1.example.comgroup_vars/

IN THIS LESSON

Step 1 of 7

STEP 1 / 7

An inventory is a list of hosts

The simplest inventory is a text file with one host per line. Ansible reads it to learn which machines it may manage.

web1.example.com
web2.example.com
db1.example.com

← → step · R replay · Space play/pause

YOU'LL LEARN

  • How hosts and groups are written in an inventory
  • How group variables avoid repetition
  • How to target a group from the command line
  • What a dynamic inventory is
  1. An inventory is a list of hosts. The simplest inventory is a text file with one host per line. Ansible reads it to learn which machines it may manage.
  2. Group hosts by role. Put hosts under group names in square brackets. Groups let you act on every machine of one kind at once, such as all web servers.
  3. More groups, same file. A host can belong to several groups, and groups can contain other groups. That lets you slice your estate by role, environment or location without duplicating hosts.
  4. Set values per group. Variables that apply to a whole group live in a group_vars folder, in a file named after the group. Every host in that group picks them up automatically.
  5. Target a group. On the command line, name a group to run against only those machines. The same works in a playbook through its hosts line.
  6. Dynamic inventory. In the cloud, machines come and go, so a hand-written file goes stale. Inventory plugins ask the cloud provider's API for the current list of machines, and can group them by tags.
  7. Recap. The inventory is Ansible's map of your machines.