Pyinfra – agentless infrastructure automation, in plain Python

gregnavis1 pts0 comments

pyinfra — agentless infrastructure automation, in plain Python

★ >5.7k<br>$ quickstart →

┌─ pyinfra v3 ──────────────┐

> automate<br>thousands of<br>servers_

pyinfra is a python-native, agentless automation tool that runs<br>commands over ssh — concurrently, idempotently, and<br>6× faster than ansible .

uv tool install pyinfra<br>copy

[mit license]<br>[python 3.10+]<br>[no agents]<br>[zero config]

[ read the docs ]<br>[ view on github ]

deploy.py<br>inventory.py<br>output

1# deploy.py · 23 hosts<br>3from pyinfra.operations import apt, files, systemd<br>5apt.packages(<br>6 packages=["nginx", "certbot"],<br>7 update=True,<br>8)<br>10files.template(<br>11 src="templates/nginx.conf.j2",<br>12 dest="/etc/nginx/sites-enabled/api",<br>13)<br>14<br>15systemd.service("nginx", reloaded=True)

1# inventory.py · groups + hosts<br>3web = [<br>4 ("web-01.prod", {"role": "edge"}),<br>5 ("web-02.prod", {"role": "edge"}),<br>6 *[(f"web-{i:02}.prod", {}) for i in range(3, 24)],<br>7]<br>9db = [<br>10 ("db-01.prod", {"role": "primary"}),<br>11 ("db-02.prod", {"role": "replica"}),<br>12]<br>13<br>14# $ pyinfra inventory.py deploy.py --limit web<br>15# → 23 hosts targeted

$ pyinfra inventory.py deploy.py --limit web

--> Loading inventory…<br>Hosts: web-01..web-23

--> Gathering facts (concurrent)…<br>23 hosts · 0.6s

--> Running deploy.py…<br>✓ web-01.prod 3 ops changed=2 0.42s<br>✓ web-02.prod 3 ops changed=2 0.39s<br>⟳ web-03.prod running… apt.packages

--> Summary<br>successful: 23 changed: 18 failed: 0 total: 2.1s

NORMAL deploy.py python<br>23 hosts ready · --dry · 17:42

// streaming output

See what changes,<br>before it changes.

Run with --dry for a per-host diff of every operation pyinfra<br>would perform. Run for real and watch results stream back in parallel.

bash · zsh · ~/ops<br>● live

$ pyinfra inventory.py deploy.py --limit web

--> Loading inventory…

Hosts: web-01..web-24, db-01..db-04

--> Gathering facts (concurrent)…

24 hosts · 0.6s

--> Running deploy.py…

✓ web-01.prod 3 ops changed=2 0.42s<br>✓ web-02.prod 3 ops changed=2 0.39s<br>✓ web-03.prod 3 ops changed=0 0.18s<br>✓ web-04.prod 3 ops changed=2 0.44s<br>⟳ web-05.prod running… apt.packages<br>… 19 more

--> Summary

successful: 24 changed: 18 no-change: 6 failed: 0<br>total: 2.1s

// features

Why pyinfra in six points.

def pure():

Just Python

No yaml. No jinja-in-yaml. Real control flow. Your editor already understands it.

def fast():

Concurrent ssh

6× faster than ansible on identical workloads. Built on gevent + SSH.

def safe():

Diff before apply

Run --dry to preview every change. Operations are idempotent — re-runs are no-ops.

def small():

0 agents

Only requirement on hosts: a shell and ssh. No daemons. No state files. No control plane.

def big():

Scale-ready

Works on 1 host or 10,000. Parallel execution, realtime streaming output.

def open():

Hackable

Custom operation in 10 lines. Connect to anything that speaks a shell — docker, lxc, k8s.

// vs ansible

$ diff ansible/ pyinfra/

--- ansible/playbook.yml<br>16 lines

- hosts: web<br>tasks:<br>- name: install nginx<br>apt:<br>name: nginx<br>update_cache: yes<br>- name: render config<br>template:<br>src: nginx.conf.j2<br>dest: /etc/nginx/sites-enabled/api<br>notify: reload nginx<br>handlers:<br>- name: reload nginx<br>service:<br>name: nginx<br>state: reloaded

+++ pyinfra/deploy.py<br>8 lines

from pyinfra.operations import apt, files, systemd

apt.packages(["nginx"], update=True)

cfg = files.template(<br>src="nginx.conf.j2",<br>dest="/etc/nginx/sites-enabled/api",<br>if cfg.will_change:<br>systemd.service("nginx", reloaded=True)

// manifesto

#01

Code > config

A loop is just a loop. Stop encoding control flow into yaml.

#02

Show, then do

Diff first. Apply second. Surprise nobody.

#03

Stay out of the way

No agent. No state file. No control plane. SSH and go.

#04

Read like english

Operations are nouns and verbs. apt.packages. files.template. systemd.service.

// 180+ contributors

Thank you to hundreds open source contributors from companies and institutes all over...

01SAP

02EPAM Systems

03Lawrence Livermore

04Utrecht University

05Odoo

06Rochester Inst. of Tech.

07Linköping University

08Paul Scherrer Institute

09Iress

10NPR

11Fox-IT

12Prezi

13Sensorfact

14EDITED

15Cynerio

# ready when you are

$ uv tool install pyinfra_

Read the 5-minute quickstart. Deploy your first host today. Replace your ansible repo next quarter.

[ quickstart → ]<br>[ github ↗ ]

nginx pyinfra prod deploy hosts changed

Related Articles