Skip to content

Lesson 4 of 9 · 24m

Inventory Design That Survives Growth

Model environments, groups, host variables, and dynamic inventory so targeting remains precise as the fleet expands.

A small inventory is a list of machines. A mature inventory is a database of operational intent. If webserver-17 is both a hostname and a policy container, you are one requisition away from chaos. Good inventory design separates where the host is, what role it performs, and which environment or blast radius it belongs to.

Name the dimensions you target

Most teams need at least these axes:

[web]
web-01.use1.sudosecurity.org
web-02.use1.sudosecurity.org

[database]
db-01.use1.sudosecurity.org

[production:children]
web
database

[use1:children]
web
database

Groups describe roles, environments, regions, and failure domains. A host can belong to several. The mistake is encoding all of that in the hostname and then writing fragile regex limits later. Group membership should be explicit enough that another engineer can predict targeting without guessing.

Put data where ownership is clear

group_vars and host_vars are not dumping grounds. They are ownership boundaries:

inventory/
  production/
    hosts.ini
    group_vars/
      all.yml
      web.yml
      database.yml
    host_vars/
      db-01.use1.sudosecurity.org.yml

all.yml contains fleet-wide policy. web.yml contains web-role policy. Host files contain machine-specific exceptions that cannot be generalized yet. If a value appears in three host files, it probably wants to become group data.

Dynamic inventory still needs discipline

Cloud instance tags can generate inventory automatically:

plugin: amazon.aws.aws_ec2
regions:
  - us-east-1
keyed_groups:
  - key: tags.Role
    prefix: role
  - key: tags.Environment
    prefix: env
hostnames:
  - tag:Name

Dynamic inventory solves stale host lists, not unclear ownership. Tags must be governed, required, and audited. If any engineer can create Role=web by accident, your targeting system is only as strong as your cloud tagging policy.

Avoid variable soup

Ansible has many precedence levels. High-precedence data is useful for emergencies and miserable as routine architecture. Use a small number of predictable locations:

role defaults        safe fallback behavior
group_vars           environment and role policy
host_vars            rare machine-specific truth
extra vars           operator input for this run only

When something behaves differently in production, you should know where to look first. If the answer is "anywhere in twenty-two possible layers," the design failed.

Inventory is an attack surface

Inventory often reveals hostnames, network layout, role names, cloud accounts, and occasional credentials. Restrict access, scan for secrets, and never let convenience turn it into a shadow password manager. Vault belongs in the next lesson; embarrassment belongs here.

Field notes

  • Never use overlapping environment group names across inventories unless the targeting boundary is explicit.
  • Prefer fully qualified hostnames or stable cloud identities over nicknames like db1.
  • Test --limit patterns in check mode before large runs; inventory mistakes are production incidents.
  • Dynamic inventory caching improves speed but can make freshly terminated hosts look alive.
  • Keep inventory generation deterministic enough that two operators do not target different fleets by accident.

Was this lesson helpful?

Sign in to rate