Use this file to discover all available pages before exploring further.
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.
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:
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
from pyinfra.operations import apt, files, server# Update package lists and upgrade all packagesapt.update( name="Update apt cache", _sudo=True,)# Install essential packagesapt.packages( name="Install essential tools", packages=["vim", "git", "htop", "nginx"], update=True, _sudo=True,)# Create a simple nginx landing pagefiles.put( name="Deploy custom index.html", src="index.html", dest="/var/www/html/index.html", mode="644", _sudo=True,)# Ensure nginx is runningserver.service( name="Ensure nginx is running", service="nginx", running=True, enabled=True, _sudo=True,)
2
Create the index.html file
Create index.html in the same directory:
index.html
<!DOCTYPE html><html><head> <title>pyinfra deployed this!</title></head><body> <h1>Hello from pyinfra!</h1> <p>This page was deployed using pyinfra.</p></body></html>
3
Run the deploy
Execute your deploy file:
pyinfra @docker/ubuntu deploy.py
You’ll see output showing each operation and what changes were made:
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
# Define your hoststargets = [ "@docker/ubuntu", # Docker container "web1.example.com", # Production web server ("web2.example.com", {"ssh_port": 2222}), # Server with custom SSH port]# Define groups for organizing hostswebservers = [ "web1.example.com", "web2.example.com",]containers = [ "@docker/ubuntu",]
Now run your deploy against the inventory:
pyinfra inventory.py deploy.py
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:
Need to see exactly what’s happening? Use verbose output:
# Show operation detailspyinfra inventory.py deploy.py -v# Show commands being executedpyinfra inventory.py deploy.py -vv# Show all output including stderrpyinfra inventory.py deploy.py -vvv