How to automate Linux server provisioning with PyInfra 3.8.0 on Ubuntu 24.04

Why PyInfra 3.8.0 for Linux Server Provisioning

PyInfra is an agentless infrastructure automation tool that lets you provision and configure Linux servers using pure Python. Unlike Ansible or Puppet, PyInfra executes directly over SSH without requiring any agent installation on target machines. With the release of PyInfra 3.8.0, the tool brings improved performance, better error handling, and enhanced compatibility with modern Linux distributions.

If you're managing multiple Ubuntu servers and want to avoid Terraform's verbosity or CloudFormation's AWS-only limitations, PyInfra 3.8.0 offers a refreshing alternative that feels native to Python developers.

Prerequisites and Setup

Before deploying with PyInfra 3.8.0, ensure you have:

  • Local machine: Python 3.8+ installed
  • Target servers: Ubuntu 24.04 LTS with SSH access
  • SSH keys: Configured for passwordless authentication
  • Network: SSH connectivity from your local machine to target servers

Installing PyInfra 3.8.0

pip install pyinfra==3.8.0

Verify the installation:

pyinfra --version

You should see pyinfra, version 3.8.0 or similar output.

Creating Your First Deployment

PyInfra uses a deploy.py file structure where you define infrastructure using Python operations. Here's a minimal setup for Ubuntu 24.04:

# deploy.py
from pyinfra.operations import apt, files, systemd

# Update package manager
apt.update()

# Install essential packages
apt.packages(
    name="Install web server stack",
    packages=["nginx", "curl", "git", "python3-pip"],
)

# Create application directory
files.directory(
    name="Create app directory",
    path="/opt/myapp",
    user="ubuntu",
    group="ubuntu",
    mode="755",
)

# Enable and start Nginx
systemd.service(
    name="Start Nginx",
    service="nginx",
    enabled=True,
    running=True,
)

This deploy script:

  1. Updates the APT package cache
  2. Installs Nginx, curl, git, and Python pip
  3. Creates an application directory with proper permissions
  4. Enables and starts the Nginx service

Deploying to Ubuntu Servers

Define your inventory in an inventory.py file:

# inventory.py
from pyinfra.inventory import host

servers = [
    host("192.168.1.10", name="web-server-1"),
    host("192.168.1.11", name="web-server-2"),
]

Execute the deployment:

pyinfra inventory.py deploy.py

PyInfra will:

  1. Connect via SSH to each server
  2. Execute operations in sequence
  3. Report success/failure for each step
  4. Provide detailed logs for debugging

Advanced Configuration with PyInfra 3.8.0

Conditional Operations

Run operations only on specific conditions:

from pyinfra.operations import shell, apt
from pyinfra import host

# Check Ubuntu version and install accordingly
if host.data.os == "Ubuntu" and "24.04" in host.data.os_version:
    apt.packages(packages=["ubuntu-24.04-specific-package"])

# Run command only on specific hosts
if "web" in host.name:
    shell.command(name="Start web services", commands=["systemctl start nginx"])

Error Handling and Idempotency

PyInfra 3.8.0 excels at idempotency—running the same deploy multiple times won't cause issues:

from pyinfra.operations import apt, files

# Safe to run repeatedly
apt.packages(
    name="Ensure packages installed",
    packages=["postgresql-15", "postgresql-contrib"],
)

# Creates file only if it doesn't exist
files.template(
    name="Deploy config",
    src="templates/nginx.conf",
    dest="/etc/nginx/nginx.conf",
    mode="644",
)

Comparison: PyInfra vs Alternatives for Ubuntu Provisioning

| Feature | PyInfra 3.8 | Ansible | Terraform | CloudFormation | |---------|-------------|---------|-----------|----------------| | Language | Python | YAML | HCL | JSON/YAML | | Agent Required | No | No | No | N/A (AWS only) | | Learning Curve | Low (Python) | Medium | Medium | Medium | | Multi-Cloud | Yes | Yes | Yes | AWS only | | Idempotent | Yes | Yes | Yes | Yes | | SSH Only | Yes | Yes | No | No | | Performance | Fast | Medium | Varies | Slow | | Community Size | Small | Large | Large | Large |

Choose PyInfra if you want Python-native infrastructure code without YAML boilerplate. Choose Ansible if you need larger community resources. Choose Terraform for multi-cloud infrastructure state management.

Common Pitfalls and Solutions

SSH Connection Timeouts

Ensure SSH connectivity before running deployments:

ssh -i ~/.ssh/id_rsa ubuntu@192.168.1.10 "uname -a"

If this fails, PyInfra will too. Check firewall rules and SSH key permissions.

Python Path Issues on Target Servers

PyInfra 3.8.0 expects Python on the target system. Ubuntu 24.04 includes Python 3.12 by default, but older Ubuntu versions may need explicit Python installation:

from pyinfra.operations import apt

apt.packages(
    name="Install Python 3",
    packages=["python3"],
)

Privilege Escalation

For operations requiring sudo, use the sudo parameter:

from pyinfra.operations import apt, systemd

apt.packages(
    name="Install services",
    packages=["docker.io"],
    sudo=True,
)

systemd.service(
    name="Start Docker",
    service="docker",
    running=True,
    sudo=True,
)

Testing Your Deployment Locally

Use the --dry flag to preview changes without executing:

pyinfra inventory.py deploy.py --dry

This outputs all operations that would run, letting you validate logic before touching production servers.

Production Deployment Best Practices

  1. Version Control: Store all deploy.py and inventory.py files in Git
  2. Idempotency Testing: Run deployments twice to ensure no side effects
  3. Staging Environment: Test on staging Ubuntu servers before production
  4. SSH Key Management: Use SSH agent or configuration files, never hardcode keys
  5. Logging: Capture deployment logs for audit trails

Conclusion

PyInfra 3.8.0 simplifies Linux server provisioning for Python developers. By using pure Python instead of YAML, you get type hints, IDE autocomplete, and the full Python ecosystem at your disposal. Whether you're provisioning a single Ubuntu server or scaling to dozens, PyInfra's agentless architecture keeps your infrastructure lean and fast.

Recommended Tools