The yum module manages packages and repositories on RHEL/CentOS systems using the yum package manager.
Yum package names are case-sensitive .
Functions
packages
Install, remove, or update yum packages.
from pyinfra.operations import yum
yum.packages(
name = "Install web server" ,
packages = [ "nginx" , "certbot" ],
update = True ,
)
List of packages to ensure.
Whether the packages should be installed.
Whether to upgrade packages without a specified version.
Run yum update before installing packages.
Run yum clean all before installing packages.
Add the —nobest option to install.
Additional arguments to the yum install command.
Additional arguments to the yum uninstall command.
Package versions can be pinned as: <pkg>=<version>
Examples
Basic Installation
Latest Versions
With Clean
from pyinfra.operations import yum
# Update package list and install packages
yum.packages(
name = "Install Vim and Vim enhanced" ,
packages = [ "vim-enhanced" , "vim" ],
update = True ,
)
update
Update all yum packages.
from pyinfra.operations import yum
yum.update(
name = "Update all packages" ,
)
This operation is not idempotent - it will always execute.
repo
Add, remove, or update yum repositories.
from pyinfra.operations import yum
yum.repo(
name = "Add Docker repository" ,
src = "DockerCE" ,
baseurl = "https://download.docker.com/linux/centos/7/$basearch/stable" ,
)
URL or name for the .repo file.
Whether the .repo file should be present.
The baseurl of the repo (if src is not a URL).
Optional verbose description.
Whether this repo is enabled.
Whether to set gpgcheck=1.
The URL to the GPG key for this repo.
baseurl, description, gpgcheck, and gpgkey are only valid when src is a filename (not a URL). Use a URL to download and install remote repository files.
Examples
From URL
Manual Construction
from pyinfra.operations import yum
# Download a repository file
yum.repo(
name = "Install Docker-CE repo via URL" ,
src = "https://download.docker.com/linux/centos/docker-ce.repo" ,
)
rpm
Install or remove .rpm package files.
from pyinfra import host
from pyinfra.operations import yum
from pyinfra.facts.server import LinuxDistribution
major_version = host.get_fact(LinuxDistribution)[ "major" ]
yum.rpm(
name = "Install EPEL rpm to enable EPEL repo" ,
src = f "https://dl.fedoraproject.org/pub/epel/epel-release-latest- { major_version } .noarch.rpm" ,
)
Filename or URL of the .rpm package.
Whether the package should exist on the system.
URL sources with present=False : If the .rpm file isn’t downloaded, pyinfra can’t remove any existing package as the file won’t exist until mid-deploy.
key
Add yum GPG keys with rpm.
from pyinfra import host
from pyinfra.operations import yum
from pyinfra.facts.server import LinuxDistribution
linux_id = host.get_fact(LinuxDistribution)[ "release_meta" ].get( "ID" )
yum.key(
name = "Add the Docker CentOS gpg key" ,
src = f "https://download.docker.com/linux/ { linux_id } /gpg" ,
)
Filename or URL of the GPG key.
This operation is not idempotent - it always returns one command without state checking.
Common Use Cases
Install EPEL Repository
from pyinfra import host
from pyinfra.operations import yum
from pyinfra.facts.server import LinuxDistribution
# Get the major version
major_version = host.get_fact(LinuxDistribution)[ "major" ]
# Install EPEL release package
yum.rpm(
name = "Install EPEL repository" ,
src = f "https://dl.fedoraproject.org/pub/epel/epel-release-latest- { major_version } .noarch.rpm" ,
)
# Install packages from EPEL
yum.packages(
name = "Install packages from EPEL" ,
packages = [ "htop" , "ncdu" ],
)
Install Docker on CentOS
from pyinfra import host
from pyinfra.operations import yum
from pyinfra.facts.server import LinuxDistribution
linux_id = host.get_fact(LinuxDistribution)[ "release_meta" ].get( "ID" )
# Add Docker GPG key
yum.key(
name = "Add Docker GPG key" ,
src = f "https://download.docker.com/linux/ { linux_id } /gpg" ,
)
# Add Docker repository
yum.repo(
name = "Add Docker repository" ,
src = "https://download.docker.com/linux/centos/docker-ce.repo" ,
)
# Install Docker
yum.packages(
name = "Install Docker" ,
packages = [ "docker-ce" , "docker-ce-cli" , "containerd.io" ],
)
Development Environment
from pyinfra.operations import yum
# Install development tools
yum.packages(
name = "Install Development Tools group" ,
packages = [ "@Development Tools" ],
)
# Install additional tools
yum.packages(
name = "Install development utilities" ,
packages = [
"git" ,
"vim" ,
"tmux" ,
"wget" ,
"curl" ,
],
update = True ,
)