What Are Operations?
Operations are Python functions that:- Define the desired state of a system (e.g., “package X should be installed”)
- Generate shell commands to achieve that state
- Are idempotent - running them multiple times produces the same result
- Can detect changes before applying them (dry-run mode)
Basic Operation Usage
Importing Operations
Operations are organized by category in thepyinfra.operations module:
Calling Operations
Every operation call should include a descriptivename parameter:
The
name parameter is required and helps you identify operations in pyinfra’s output.Common Operations
Package Management
File Operations
System Operations
Git Operations
Global Arguments
Global arguments (prefixed with_) control how operations are executed:
Common Global Arguments
Global arguments are prefixed with underscore (
_) to distinguish them from operation-specific parameters.Operation Return Values
Operations return metadata that you can use for conditional execution:Working with Templates
pyinfra uses Jinja2 for templating:1
Create a template file
Create
templates/nginx.conf.j2:templates/nginx.conf.j2
2
Use template in deploy
deploy.py
Combining Operations
Build complex deployments by combining multiple operations:deploy.py
Operation Categories
pyinfra includes operations for many common tasks:Best Practices
Next Steps
- Inventory and Data - Pass configuration data to operations
- Deploying Applications - Build complete deployments
- Idempotent Operations - Deep dive into state management
- Parallel Execution - Scale to many hosts
