The dnf module manages packages and repositories on Fedora and RHEL 8+ systems using the dnf package manager.
Dnf package names are case-sensitive .
Functions
packages
Install, remove, or update dnf packages.
from pyinfra.operations import dnf
dnf.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 dnf update before installing packages.
Run dnf clean before installing packages.
Add the —nobest option to install.
Additional arguments to the dnf install command.
Additional arguments to the dnf uninstall command.
Package versions can be pinned as: <pkg>=<version>
Examples
Basic Installation
Latest Versions
With Clean
from pyinfra.operations import dnf
# Update package list and install packages
dnf.packages(
name = "Install Vim and Vim enhanced" ,
packages = [ "vim-enhanced" , "vim" ],
update = True ,
)
update
Update all dnf packages.
from pyinfra.operations import dnf
dnf.update(
name = "Update all packages" ,
)
This operation is not idempotent - it will always execute.
repo
Add, remove, or update dnf repositories.
from pyinfra.operations import dnf
dnf.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 dnf
# Download a repository file
dnf.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 dnf
from pyinfra.facts.server import LinuxDistribution
major_centos_version = host.get_fact(LinuxDistribution)[ "major" ]
dnf.rpm(
name = "Install EPEL rpm to enable EPEL repo" ,
src = f "https://dl.fedoraproject.org/pub/epel/epel-release-latest- { major_centos_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 dnf GPG keys with rpm.
from pyinfra import host
from pyinfra.operations import dnf
from pyinfra.facts.server import LinuxDistribution
linux_id = host.get_fact(LinuxDistribution)[ "release_meta" ].get( "ID" )
dnf.key(
name = "Add the Docker 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 dnf
from pyinfra.facts.server import LinuxDistribution
# Get the major version
major_version = host.get_fact(LinuxDistribution)[ "major" ]
# Install EPEL release package
dnf.rpm(
name = "Install EPEL repository" ,
src = f "https://dl.fedoraproject.org/pub/epel/epel-release-latest- { major_version } .noarch.rpm" ,
)
# Install packages from EPEL
dnf.packages(
name = "Install packages from EPEL" ,
packages = [ "htop" , "ncdu" ],
)
Install Docker on Fedora
from pyinfra import host
from pyinfra.operations import dnf
from pyinfra.facts.server import LinuxDistribution
linux_id = host.get_fact(LinuxDistribution)[ "release_meta" ].get( "ID" )
# Add Docker GPG key
dnf.key(
name = "Add Docker GPG key" ,
src = f "https://download.docker.com/linux/ { linux_id } /gpg" ,
)
# Add Docker repository
dnf.repo(
name = "Add Docker repository" ,
src = "https://download.docker.com/linux/fedora/docker-ce.repo" ,
)
# Install Docker
dnf.packages(
name = "Install Docker" ,
packages = [ "docker-ce" , "docker-ce-cli" , "containerd.io" ],
)
System Upgrade
from pyinfra.operations import dnf
# Clean metadata
dnf.packages(
name = "Clean dnf cache" ,
packages = [],
clean = True ,
)
# Update all packages
dnf.update(
name = "Update all packages" ,
)
Development Environment
from pyinfra.operations import dnf
# Install development tools
dnf.packages(
name = "Install Development Tools group" ,
packages = [ "@development-tools" ],
)
# Install additional tools
dnf.packages(
name = "Install development utilities" ,
packages = [
"git" ,
"vim" ,
"tmux" ,
"wget" ,
"curl" ,
],
update = True ,
)