Skip to main content
The server module takes care of os-level state. Targets POSIX compatibility, tested on Linux/BSD.

Functions

server.reboot

Reboot the server and wait for reconnection.
server.reboot(
    delay=10,
    interval=1,
    reboot_timeout=300,
)

server.wait

Waits for a port to come active on the target machine. Requires netstat, checks every second.
server.wait(port)

server.shell

Run raw shell code on server during a deploy.
server.shell(commands)

server.script

Upload and execute a local script on the remote host.
server.script(
    src,
    args=(),
)

server.script_template

Generate, upload and execute a local script template on the remote host.
server.script_template(
    src,
    args=(),
    **data
)

server.modprobe

Load/unload kernel modules.
server.modprobe(
    module,
    present=True,
    force=False,
)

server.mount

Manage mounted filesystems.
server.mount(
    path,
    mounted=True,
    options=None,
    device=None,
    fs_type=None,
)
This operation does not attempt to modify the on disk fstab file - for that you should use the files.line operation.

server.hostname

Set the system hostname using hostnamectl or hostname on older systems.
server.hostname(
    hostname,
    hostname_file=None,
)

server.timezone

Set the system timezone.
server.timezone(timezone)

server.sysctl

Edit sysctl configuration.
server.sysctl(
    key,
    value,
    persist=False,
    persist_file="/etc/sysctl.conf",
)

server.service

Manage the state of services. This command checks for the presence of all the Linux init systems pyinfra can handle and executes the relevant operation.
server.service(
    service,
    running=True,
    restarted=False,
    reloaded=False,
    command=None,
    enabled=None,
)

server.packages

Add or remove system packages. This command checks for the presence of all the system package managers pyinfra can handle and executes the relevant operation.
server.packages(
    packages,
    present=True,
)

server.group

Add/remove system groups.
server.group(
    group,
    present=True,
    system=False,
    gid=None,
)

server.user

Add/remove/update system users & their ssh authorized_keys.
server.user(
    user,
    present=True,
    home=None,
    shell=None,
    group=None,
    groups=None,
    append=False,
    public_keys=None,
    delete_keys=False,
    ensure_home=True,
    create_home=False,
    system=False,
    uid=None,
    comment=None,
    unique=True,
    password=None,
)

server.locale

Enable/Disable locale.
server.locale(
    locale,
    present=True,
)

Examples

from pyinfra.operations import server

# Reboot the server
server.reboot(
    name="Reboot the server and wait to reconnect",
    delay=60,
    reboot_timeout=600,
)

# Run shell commands
server.shell(
    name="Run lxd auto init",
    commands=["lxd init --auto"],
)

# Ensure a user exists
server.user(
    name="Ensure myweb user exists",
    user="myweb",
    shell="/bin/bash",
)

# Set timezone
server.timezone(
    name="Set the timezone to Europe/Amsterdam",
    timezone="Europe/Amsterdam",
)

Build docs developers (and LLMs) love