Prerequisites
Before you begin, ensure you have:- Python 3.10 or higher installed
- SSH access to a server (or use Docker/local execution)
- Basic familiarity with Python and command line
Installation
Install pyinfra using your preferred package manager:If you see a version number, you’re ready to go!
Your First Command
Let’s run a simple command on a target. We’ll use a Docker container for this example:- Targets a Docker container (
@docker/ubuntu) - Executes a shell command (
exec) - Runs
echo "Hello from pyinfra!"
Understanding the syntax
Understanding the syntax
@docker/ubuntu: The target connector and imageexec: The pyinfra command to execute--: Separates pyinfra arguments from the shell commandecho "Hello from pyinfra!": The actual command to run
Try Different Targets
Using Operations
Commands are great for ad-hoc tasks, but pyinfra really shines with operations. Operations are declarative and idempotent. Let’s install a package using theapt.packages operation:
- Installs the
iftoppackage - Updates the apt cache first (
update=true) - Runs with sudo privileges (
_sudo=true) - Only makes changes if
iftopisn’t already installed
Run this command twice - notice the second time it says “no changes”? That’s idempotency in action.
Creating Your First Deploy File
For anything more complex than a single operation, you’ll want to create a deploy file. This is a Python file that defines your infrastructure.1
Create a deploy file
Create a file called
deploy.py:deploy.py
2
Create the index.html file
Create
index.html in the same directory:index.html
3
Run the deploy
Execute your deploy file:You’ll see output showing each operation and what changes were made:
4
Verify it worked
Check that nginx is serving your page:You should see your HTML page!
Creating an Inventory File
Typing host names on the command line works for quick tasks, but for real deployments you’ll want an inventory file. Createinventory.py:
inventory.py
deploy.py on all hosts defined in inventory.py.
Dry Run Mode
Before making changes to production servers, use dry run mode to see what would happen:Debugging
Need to see exactly what’s happening? Use verbose output:Using Python Features
Remember, deploy files are just Python! Use all the language features you know:deploy.py
Understanding the Workflow
Here’s what happens when you run a deploy:1
Load inventory
pyinfra loads your inventory file and connects to all target hosts.
2
Gather facts
For each host, pyinfra gathers facts (OS, packages, files, etc.) to understand current state.
3
Generate operations
Your deploy file is executed, generating a list of operations to perform.
4
Generate commands
Each operation compares desired state with current state and generates the minimal shell commands needed.
5
Execute in parallel
Commands are executed on all hosts in parallel for maximum speed.
6
Report results
pyinfra shows you what changed (or didn’t) on each host.
Common Operations
Here are some operations you’ll use frequently:Package Management
Package Management
File Management
File Management
Service Management
Service Management
Running Commands
Running Commands
Next Steps
You’ve learned the basics of pyinfra! Here’s where to go next:Core Concepts
Deep dive into how pyinfra works
Operations Reference
Explore all 40+ operation modules
Inventory & Data
Learn advanced inventory management
CLI Reference
Master the pyinfra command line
Need Help?
GitHub Discussions
Ask questions and share knowledge
Matrix Chat
Real-time community support
Examples Repository
See real-world examples
Issue Tracker
Report bugs and request features
