Skip to main content
This guide will walk you through installing pyinfra and running your first deployment. You’ll learn the core workflow and deploy a simple web server configuration.

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
Don’t have a server? No problem! You can use @docker/ubuntu or @local to test pyinfra on your local machine.

Installation

Install pyinfra using your preferred package manager:
Verify the installation:
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:
This command:
  • Targets a Docker container (@docker/ubuntu)
  • Executes a shell command (exec)
  • Runs echo "Hello from pyinfra!"
  • @docker/ubuntu: The target connector and image
  • exec: The pyinfra command to execute
  • --: Separates pyinfra arguments from the shell command
  • echo "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 the apt.packages operation:
This operation:
  • Installs the iftop package
  • Updates the apt cache first (update=true)
  • Runs with sudo privileges (_sudo=true)
  • Only makes changes if iftop isn’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. Create inventory.py:
inventory.py
Now run your deploy against the inventory:
This will execute deploy.py on all hosts defined in inventory.py.
You can limit execution to specific hosts or groups using the --limit flag:

Dry Run Mode

Before making changes to production servers, use dry run mode to see what would happen:
This shows all the commands that would be executed without actually running them.
Always test with --dry before deploying to production!

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:

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