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
deploy.py
Group Data
Data specific to a group of hosts:inventory.py
deploy.py
Host Data
Data specific to individual hosts:inventory.py
deploy.py
Data Precedence
Data is resolved in order of specificity:- Host data (highest priority)
- Group data
- 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:Dynamic Inventory
Generate inventory programmatically:inventory.py
Using Host Facts
Access system information via facts:deploy.py
Real-World Example
Complete inventory for a web application:inventory.py
deploy.py
Best Practices
Next Steps
- Deploying Applications - Build complete deployments using inventory and data
- Using Operations - Learn how to use data in operations
- Idempotent Operations - Understand state management
