Overview
The Inventory is pyinfra’s system for managing target hosts, organizing them into groups, and associating configuration data. It’s the foundation of multi-host deployments and enables flexible, data-driven infrastructure automation.The Inventory class (defined in
src/pyinfra/api/inventory.py) represents a collection of hosts with their associated data and group memberships.Inventory Structure
The Inventory consists of three key components:- Hosts: Individual target machines
- Groups: Collections of hosts sharing properties
- Data: Configuration values associated with hosts and groups
Creating Inventories
Simple Inventory
Create an inventory with a list of hostnames:Inventory with Host Data
Associate data with specific hosts:Inventory with Groups
Organize hosts into groups:Inventory with Global Data
Set default values for all hosts:Host Creation
When the inventory is created, it generates Host objects:Host Object
Each host in the inventory is represented by a Host object (fromsrc/pyinfra/api/host.py):
Data Hierarchy
pyinfra uses a waterfall data system where more specific data overrides more general data:Accessing Host Data
Data Example
Inventory Methods
Getting Hosts
Getting Groups
Getting Data
Inventory Connectors
pyinfra supports connector-based inventory sources:SSH Connector (Default)
Local Connector
Docker Connector
Dynamic Connectors
Connectors can generate multiple hosts:CLI Inventory
When using pyinfra from the command line, inventory is specified differently:Inventory File
Inline Hosts
Inventory + Data Files
Limiting Hosts
Run operations on a subset of inventory:Active vs Activated Hosts
Inventory Patterns
Environment-Based Inventory
Role-Based Groups
Dynamic Inventory
Conditional Deployment
Host Data Patterns
Port Mapping
Connection Configuration
Application Configuration
Override Data
Override inventory data from the command line:Best Practices
Organize by Role
Group hosts by their function (webservers, databases, etc.) for targeted deployments.
Use Data Hierarchy
Place common config in global data, role-specific in groups, and unique values in host data.
Environment Separation
Use separate inventory files for production, staging, and development environments.
Descriptive Names
Use clear, consistent naming for hosts and groups (e.g.,
web1.prod not server23).Document Data Keys
Document expected data keys and their purposes for your deployments.
Version Control
Keep inventory files in version control to track infrastructure changes.
Testing Inventory
Related Concepts
Host
Learn about the Host class and host-specific operations
Connectors
Understand how connectors enable different inventory sources
State
See how State manages active and failed hosts
CLI Usage
Learn command-line inventory options
