pyinfra

3.7 · active · verified Thu Apr 16

pyinfra is an infrastructure automation tool that transforms Python code into shell commands, executing them on remote servers, local machines, or Docker containers. It offers fast, agentless deployments and scales from single servers to thousands, aiming to be a Python-based alternative to YAML-centric tools like Ansible. It is currently at version 3.7 and maintains an active development and release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple pyinfra deploy to install and configure Nginx on a target. Save this code as `deploy.py`. To execute it, you would typically run `pyinfra <inventory> deploy.py` from your terminal. For local testing, use `pyinfra @local deploy.py`. To target a remote server via SSH, use `pyinfra my-server.net deploy.py --ssh-user <your_user>`. The `apt.update` and `apt.packages` operations ensure idempotency, meaning they only make changes if necessary.

import os
from pyinfra.operations import apt, files

# Define target hosts (using @local for a quick test)
# For remote SSH, change to: hosts = ['my-server.net']
# For Docker, change to: hosts = ['@docker/ubuntu:latest']

# Create a dummy inventory file if running with `pyinfra inventory.py deploy.py`
# In a real scenario, this would connect to actual hosts.
# If running directly, pyinfra will prompt or use @local if no inventory is specified.

# deploy.py

# Ensure apt packages are updated and Nginx is installed
apt.update(name="Update apt repositories", _sudo=True)
apt.packages(name="Install Nginx", packages=['nginx'], _sudo=True)

# Ensure Nginx service is running and enabled
files.file(
    name="Ensure Nginx config is present",
    path="/etc/nginx/sites-available/default",
    contents="""
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name _;
    location / {
        try_files $uri $uri/ =404;
    }
}
""",
    _sudo=True
)

apt.update(
    name="Run apt update (idempotent)",
    _sudo=True
)

apt.packages(
    name="Install Nginx (idempotent)",
    packages=["nginx"],
    _sudo=True
)

# To make this runnable directly as a Python script for illustration:
# This part is typically handled by the `pyinfra` CLI.
# We will simulate a direct execution for simplicity, but real use is via `pyinfra <inventory> <deploy>`.
# import sys
# from pyinfra.api.local import Local
# with Local(['@local']) as state:
#     # In a real deploy.py, the operations would run directly.
#     # For this quickstart, we just define them as above.
#     print("Deploy operations defined. Run with: pyinfra @local deploy.py")
#     print("Or via SSH: pyinfra my-host.net deploy.py")

# Example of how to execute from CLI:
# Create a file named 'deploy.py' with the above content.
# Then run: pyinfra @local deploy.py
# Or to a remote host: pyinfra my-server.net deploy.py --ssh-user <your_user> --ssh-password "$PYINFRA_SSH_PASSWORD"

view raw JSON →