Skip to main content
Inventory is how you tell pyinfra which hosts to target and what configuration data to use. This guide covers everything from simple host lists to complex multi-environment setups with groups and data hierarchies.

What is Inventory?

Inventory in pyinfra consists of:
  • Hosts - Target machines (SSH servers, Docker containers, local machine)
  • Groups - Logical collections of hosts (web servers, databases, etc.)
  • Data - Configuration values that can be assigned to hosts, groups, or globally

Basic Inventory

Simple Host List

The simplest inventory is a list of hostnames:
inventory.py

Hosts with Connection Parameters

Configure SSH connection settings per host:
inventory.py
The tuple format (hostname, {config}) allows you to specify connection parameters for individual hosts.

Using Connectors

pyinfra supports multiple connectors for different target types:

Groups

Groups organize hosts into logical collections:
inventory.py

Targeting Specific Groups

Use groups in your deploy files for conditional logic:
deploy.py

Working with Data

Data allows you to inject configuration values into your deploys.

Global Data

Data available to all hosts:
inventory.py
Access in deploy:
deploy.py

Group Data

Data specific to a group of hosts:
inventory.py
Access group-specific data:
deploy.py

Host Data

Data specific to individual hosts:
inventory.py
Access host-specific data:
deploy.py

Data Precedence

Data is resolved in order of specificity:
  1. Host data (highest priority)
  2. Group data
  3. Global data (lowest priority)
inventory.py
Host data overrides group data, which overrides global data. Use this hierarchy to set defaults globally and override where needed.

Advanced Inventory Patterns

Multiple Environments

Create separate inventory files for each environment:
Deploy to specific environment:

Dynamic Inventory

Generate inventory programmatically:
inventory.py

Using Host Facts

Access system information via facts:
deploy.py
Facts are gathered at runtime and provide information about the target system (OS, architecture, installed packages, etc.).

Real-World Example

Complete inventory for a web application:
inventory.py
Use in deploy:
deploy.py

Best Practices

Use groups for organization - Group hosts by role, not location:
Set sensible defaults - Use global data for defaults, override in groups/hosts:
Keep secrets separate - Don’t commit secrets to version control:

Next Steps