Pyinfra: Automate Infrastructure Using Python

pyinfra automates infrastructure using Python. It’s fast and scales from one server to thousands. Great for ad-hoc command execution, service deployment, configuration management and more. Here’s why you should try pyinfra:

  • 🚀 Super fast execution over thousands of targets with predictable performance.
  • 🚨 Instant debugging with realtime stdin/stdout/stderr output (-vvv).
  • 🔄 Idempotent operations that enable diffs and dry runs before making changes.
  • 📦 Extendable with the entire Python package ecosystem.
  • 💻 Agentless execution against anything with shell access.
  • 🔌 Integrated with connectors for Docker, Terraform, Vagrant and more.


Install pyinfra with pip:

Now you can execute commands over SSH:

pyinfra exec -- echo "hello world"

Or target Docker, the local machine, and other connectors:

pyinfra @docker/ubuntu exec -- echo "Hello world"
pyinfra @local exec -- echo "Hello world"

As well as executing commands you can define state using operations:

pyinfra @docker/ubuntu apt.packages iftop update=true _sudo=true

Which can then be saved as a Python file like

from pyinfra.operations import apt

    name="Ensure iftop is installed",

And executed:

pyinfra @docker/ubuntu

Now you know the building blocks of pyinfra! By combining inventory, operations and Python code you can deploy anything.

See the more detailed getting started or using operations guides. See how to use inventory & data, global arguments and the CLI or check out the documented examples.

Related Articles

Back to top button