Managing Systems and Applications with pyinfra
Getting Facts from Your Servers
You can also use the pyinfra CLI to determine the current state of your servers using facts. For instance, if you want to compare the installed version of the vim package on several Debian and Ubuntu systems, simply run
pyinfra nov.home,podpile.home fact deb.DebPackage package=vim
For each host in the inventory, this command returns the package version or null if the package isn't installed.
Server facts, in particular, are plentiful. For instance, you can query every system's kernel version with
pyinfra nov.home,podpile.home,microos.home,code.example.com fact server.KernelVersion
Or perhaps you need to identify which systems require a reboot:
pyinfra nov.home,podpile.home,microos.home fact server.RebootRequired
If you want to query multiple facts, just append them as additional arguments to the command line (without repeating fact). Explore the Facts Index [5] to discover all facts provided by pyinfra.
Creating an Inventory File
Up to this point, you haven't seen any Python code yet. The first thing you can do is specify your hosts in an inventory file. This file, typically named inventory.py, is a Python file where you can define host groups as a list, for example:
servers = ["podpile.home", "microos.home", "code.example.com"] workstations = ["nov.home", "tux.home"]
You can now use this inventory file instead of listing all hosts on the pyinfra CLI
pyinfra inventory.py fact server.RebootRequired
With host groups defined in the inventory file, you can also restrict pyinfra commands to a specific group. For example, install the vim package solely on your workstations:
pyinfra inventory.py --sudo server.packages vim --limit workstations
Creating a Deploy
In addition to specifying your hosts and groups in an inventory file, you can also define operations for these hosts in a deploy file, typically named deploy.py. For instance, the following deploy file ensures that a list of packages is installed on all hosts:
from pyinfra.operations import server server.packages( name="Install packages", packages=["tmux", "vim"], _sudo=True )
The _sudo argument specifies that the operation runs with sudo on the host, which is necessary to install packages.
You can now run this deploy on all hosts using
pyinfra inventory.py deploy.py
However, this prompts for a sudo password for each host. You can remedy this by adding the --same-sudo-password option:
pyinfra inventory.py deploy.py --same-sudo-password
After typing the sudo password once, pyinfra reuses the same password across all hosts. If succesful, all specified packages are now installed on your hosts.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Fedora 45 Now Available
The latest iteration of Fedora has arrived with Gnome 51 and KDE Plasma 6.8.
-
Linux 7.4 is "Much Healthier" Thanks to AI
At nearly 42 million lines, Linux 7.4 is the largest merge in the kernel’s history.
-
Working Exploit for a Linux Kernel Flaw Made Public
Even before a bug received its CVE identifier or an upstream fix, the local privilege escalation vulnerability exploit found its way into the wild.
-
r/opensource Bans AI-Assisted and AI-Related Projects
The de facto subreddit for all things open source has not only banned AI-related projects but also users who "don't speak English."
-
Bottlenecks Discovered by Meta Engineers Lead to Scalability Improvements
Thanks to a small group of engineers at Meta, the Linux kernel module kernfs has been improved, which has led to some serious gains.
-
PwnEscape Poses VM Vulnerability
A guest-to-host escape flaw in Linux KVM, named PwnEscape, might be yet another means of attacking Linux.
-
Linux Mint 27 Set for a Christmas Release
The developers behind Linux Mint have announced a Christmas release, and it looks like Santa's sack will be brimming with features.
-
Is Linus Torvalds Vibe Coding?
The creator of Linux has added AI into his process for Linux kernel development, and it's more than just for debugging.
-
openKylin 3.0 Released with Expanded AI Integration
The latest release features a pre-installed AI agent to help perform tasks.
-
DigitalOcean Ends Open Source Credits Program
In a change that has surprised many open source developers, DigitalOcean has decided to sunset its Open Source Credits Program.
