Inventory class represents a collection of target hosts and provides access to groups, host data, and inventory-wide configuration.
Creating an Inventory
Create an inventory with hosts and optional groups:Constructor
tuple[list, dict]
required
Tuple containing list of host names/tuples and global data dictionary.
dict
Dictionary of data that overrides all other data sources.
dict
Named groups, each containing a tuple of (host_names, group_data).
Properties
dict[str, Host]
Dictionary mapping host names to Host objects.
dict[str, list[Host]]
Dictionary mapping group names to lists of Host objects.
dict
Global inventory data available to all hosts.
dict
Override data that takes precedence over all other data.
dict[str, dict]
Dictionary mapping host names to their host-specific data.
dict[str, dict]
Dictionary mapping group names to their group-specific data.
State
Reference to the State object (set after initialization).
Iteration
Iterate Over All Hosts
Iterate Over Active Hosts
Iterate Over Activated Hosts
Getting Hosts
Get Specific Host
Get Group
Data Access
Get Global Data
Get Override Data
Get Host Data
Get Group Data
Get Aggregated Group Data
Connectors
Inventories can use different connectors for different hosts:Groups Example
Create an inventory with multiple groups:Empty Inventory
Create an empty inventory:Complete Example
Here’s a complete example showing inventory creation and usage:Source Reference
Location:src/pyinfra/api/inventory.py:25
Key Methods
__init__()- Initialize inventory (line 45)make_hosts_and_groups()- Create hosts and groups (line 60)get_host()- Get host by name (line 185)get_group()- Get group by name (line 199)get_data()- Get global data (line 213)get_host_data()- Get host-specific data (line 227)get_group_data()- Get group data (line 234)get_groups_data()- Get aggregated group data (line 241)
