Frequently Asked Questions

How do I get the name of the current host?

The currently executing host can be fetched from the host context variable. If you need the hostname the server.Hostname fact can be used to get that:

# Get the name of the host as defined in the inventory
from pyinfra import host
name = host.name

# Get the actual current hostname from the host
from pyinfra.facts.server import Hostname
hostname = host.get_fact(Hostname)

How do I use sudo in an operation?

Sudo is controlled by one of the privilege and user escalation arguments, there are a number of additional arguments to control sudo execution:

apt.packages(
    packages=["iftop"],
    _sudo=True,
    _sudo_user="someuser",    # sudo to this user
    _use_sudo_login=True,     # use a login shell when sudo-ing
    _preserve_sudo_env=True,  # preserve the environment when sudo-ing
)