Lesson 3 / 26

Inventory: Hosts and Groups

Describe hosts, groups, group-of-groups and group variables in an INI inventory.

Tell Ansible what exists

The inventory lists the hosts Ansible manages and organises them in groups so you can target them together (web, db, prod). A group can contain other groups with :children, and :vars attaches variables to a group. Inventories can be INI or YAML files, or dynamic scripts/plugins that read from a cloud provider. Use ansible-inventory --graph to see how Ansible understands yours.

An INI inventory

ansible_connection=local is only for this demo. For real servers use their hostnames or IPs and SSH.

[web]
web1 ansible_connection=local
web2 ansible_connection=local

[db]
db1 ansible_connection=local

[prod:children]
web
db

[web:vars]
http_port=8080

How Ansible sees it

I ran ansible-inventory -i inventory.ini --graph on the file above. web and db nest inside prod.

ansible-inventory -i inventory.ini --graph

Output:

@all:
  |--@ungrouped:
  |--@prod:
  |  |--@web:
  |  |  |--web1
  |  |  |--web2
  |  |--@db:
  |  |  |--db1

Quick check: What does `[prod:children]` do in an INI inventory?

  • Encrypts the group
  • Deletes the prod group
  • Creates a new host called children
  • Makes the listed groups members of prod
Answer

Makes the listed groups members of prod — The :children suffix means the entries are groups nested under the parent.