Global Arguments¶
In addition to each operations having its own arguments, there are a number of keyword arguments available in all facts, operations and deploys.
Note
With the exception of name
these are prefixed with _
to avoid clashes with other operation and deploy arguments.
Privilege & user escalation¶
Caution
When combining privilege escalation arguments it is important to know the order they
are applied: doas
-> sudo
-> su
. For example
_sudo=True,_su_user="pyinfra"
yields a command like sudo su pyinfra..
.
Key |
Description |
Type |
Default |
---|---|---|---|
|
Execute/apply any changes with sudo. |
|
|
|
Execute/apply any changes with sudo as a non-root user. |
|
|
|
Execute sudo with a login shell. |
|
|
|
Password to sudo with. If needed and not specified pyinfra will prompt for it. |
|
|
|
Preserve the shell environment of the connecting user when using sudo. |
|
|
|
Execute/apply any changes with this user using su. |
|
|
|
Execute su with a login shell. |
|
|
|
Preserve the shell environment of the connecting user when using su. |
|
|
|
Use this shell (instead of user login shell) when using |
|
|
|
Execute/apply any changes with doas. |
|
|
|
Execute/apply any changes with doas as a non-root user. |
|
Examples:
# Execute a command with sudo
server.user(
name="Create pyinfra user using sudo",
user="pyinfra",
_sudo=True,
)
# Execute a command with a specific sudo password
server.user(
name="Create pyinfra user using sudo",
user="pyinfra",
_sudo=True,
_sudo_password="my-secret-password",
)
Shell control & features¶
Key |
Description |
Type |
Default |
---|---|---|---|
|
The shell executable to use for executing commands. |
|
|
|
Directory to switch to before executing the command. |
|
|
|
Dictionary of environment variables to set. |
|
|
|
List of exit codes to consider a success. |
|
|
|
Timeout for each command executed during the operation. |
|
|
|
Whether to get a pseudoTTY when executing any commands. |
|
|
|
String or buffer to send to the stdin of any commands. |
|
Examples:
# Execute from a specific directory
server.shell(
name="Bootstrap nginx params",
commands=["openssl dhparam -out dhparam.pem 4096"],
_chdir="/etc/ssl/certs",
)
Operation meta & callbacks¶
Key |
Description |
Type |
Default |
---|---|---|---|
|
Name of the operation. |
|
|
|
Ignore errors when executing the operation. |
|
|
|
Continue executing operation commands after error. Only applies when |
|
|
|
Only run this operation if these functions return True |
|
|
Execution strategy¶
Key |
Description |
Type |
Default |
---|---|---|---|
|
Run this operation in batches of hosts. |
|
|
|
Only execute this operation once, on the first host to see it. |
|
|
|
Run this operation host by host, rather than in parallel. |
|
|
Retry behavior¶
Retry arguments allow you to automatically retry operations that fail. You can specify how many times to retry, the delay between retries, and optionally a condition function to determine when to stop retrying. .. list-table:
:header-rows: 1
:widths: 25 45 15 15
* - Key
- Description
- Type
- Default
* - ``_retries``
- Number of times to retry failed operations.
- ``int``
- ``0``
* - ``_retry_delay``
- Delay in seconds between retry attempts.
- ``Union[int, float]``
- ``5``
* - ``_retry_until``
- Callable taking output data that returns True to continue retrying.
- ``Optional[Callable, NoneType]``
-
Examples:
# Retry a command up to 3 times with the default 5 second delay
server.shell(
name="Run flaky command with retries",
commands=["flaky_command"],
_retries=3,
)
# Retry with a custom delay
server.shell(
name="Run flaky command with custom delay",
commands=["flaky_command"],
_retries=2,
_retry_delay=10, # 10 second delay between retries
)
# Retry with a custom condition
def retry_on_specific_error(output_data):
# Retry if stderr contains "temporary failure"
for line in output_data["stderr_lines"]:
if "temporary failure" in line.lower():
return True
return False
server.shell(
name="Run command with conditional retry",
commands=["flaky_command"],
_retries=5,
_retry_until=retry_on_specific_error,
)