Command Modes
pyinfra supports five primary command modes, each designed for different use cases.Deploy Files
Execute one or more Python deploy files containing operations.Syntax
Description
Deploy files are Python scripts that define operations to execute on target hosts. All files must have a.py extension. pyinfra will load and execute each file in sequence.
Examples
Single deploy file:Behavior
- Files are executed in the order specified
- Each file can modify config, which is reset after execution
- Operations are collected during file execution and run later
- File paths are resolved relative to the current working directory (or
--chdirif specified)
Operations
Execute a single operation directly from the command line.Syntax
Description
Run a specific operation from the pyinfra operations library. The operation name must contain a dot (e.g.,server.user, apt.packages).
Examples
Create a user:Argument Formats
Positional arguments:Type Parsing
Arguments are automatically parsed:true,false→ Boolean- Numbers → Integer or float
- Strings in quotes → String
- Bare words → String
Exec (Shell Commands)
Execute arbitrary shell commands on target hosts.Syntax
Description
Run raw shell commands across all target hosts. The-- separator is required to distinguish the command from pyinfra options.
Examples
Simple command:Behavior
- Output is printed to the console in real-time
- The command is executed using
server.shelloperation - Exit codes are captured and reported
- Retries are configured via
--retryand--retry-delayoptions
Facts
Gather facts (system information) from target hosts.Syntax
Description
Collect facts about the target systems without making changes. Facts provide information about the current state of hosts.Examples
Single fact:Common Facts
server.LinuxName- Linux distribution nameserver.Users- List of system usersserver.Hostname- System hostnameserver.Os- Operating system typefiles.File- File information (requirespath=argument)files.Directory- Directory informationserver.Date- System date and time
Output
Facts are displayed in a structured format showing:- Fact name and arguments
- Results for each host
- Any errors encountered
Debug Inventory
Inspect and debug inventory configuration.Syntax
Description
Display detailed information about the inventory, including:- All hosts and their names
- Host data and variables
- Group memberships
- Connection details
Examples
Debug inventory file:Output
The command displays:- Host count
- Host names and connection information
- Data available to each host
- Group structure
Use Cases
- Verify inventory is loaded correctly
- Check host data values
- Debug group assignments
- Validate data overrides
- Test limit patterns
Command Selection Logic
pyinfra automatically determines the command mode based on the operations argument:debug-inventory→ Debug Inventory modefact→ Facts modeexec→ Exec mode- All arguments end with
.py→ Deploy Files mode - First argument contains
.→ Operations mode - Otherwise → Error (invalid operation)
Error Handling
Each command mode has specific error handling:- Deploy Files: Missing files cause immediate error
- Operations: Invalid operation names are caught on import
- Exec: Command failures are reported but execution continues
- Facts: Missing facts cause warnings, not failures
- Debug Inventory: Connection is not required
