Skip to main content
The Config class defines configuration options that control how pyinfra executes operations, connects to hosts, and handles errors.

Creating a Config

Create a config object with custom settings:

Configuration Options

Execution Control

int
default:"auto"
Number of parallel operations. Defaults to 20 × CPU cores, capped by system file limits.
int | None
default:null
Maximum percentage of hosts that can fail before stopping. None means no limit.
int
default:10
Timeout in seconds for SSH connections.

Privilege Escalation - sudo

bool
default:false
Whether to use sudo for operations.
str | None
default:null
User to sudo as. None means root.
str | None
default:null
Password for sudo authentication.
bool
default:false
Use sudo with login shell (-i flag).
bool
default:false
Preserve environment when using sudo (-E flag).

Privilege Escalation - su

str | None
default:null
User to switch to using su.
str | None
default:null
Password for su authentication.
bool
default:false
Use su with login shell (- flag).
bool
default:false
Use su with shell (-s flag).
bool
default:false
Preserve environment when using su (-p flag).

Privilege Escalation - doas

bool
default:false
Whether to use doas for operations (BSD/OpenBSD systems).
str | None
default:null
User to doas as.

Shell & Execution

str
default:"sh"
Shell to use for command execution.
bool
default:false
Show errors but don’t count as failures.
bool
default:false
Show full file diffs for file operations.

Retry Logic

int
default:0
Number of times to retry failed operations.
int
default:5
Delay in seconds between retry attempts.

Temporary Files

str | None
default:null
Temporary directory on remote hosts. None uses host’s $TMPDIR or falls back to DEFAULT_TEMP_DIR.
str
default:"/tmp"
Default temporary directory when TEMP_DIR is not set.

Version Requirements

str | None
default:null
Required pyinfra version (PEP 440 specifier).
str | list | None
default:null
Required packages (PEP 440 specifiers or requirements file path).

Using Configuration

In State

Per-Operation Override

Configuration can be overridden per operation:

In Inventory Data

Configuration can be set via inventory data:

Config Methods

Get Current State

Get all current configuration values:

Set Current State

Set configuration from state:

Lock and Reset State

Lock configuration and restore later:

Copy Config

Create a copy of the configuration:

Version Requirements

Require Specific pyinfra Version

This uses PEP 440 version specifiers:
  • ">=3.0" - Version 3.0 or higher
  • ">=3.0,<4.0" - Version 3.x only
  • "==3.1.2" - Exact version

Require Packages

Environment Variables

Pass environment variables to commands:

Complete Example

Here’s a complete example with comprehensive configuration:

Configuration Priority

Configuration is applied in this order (highest to lowest priority):
  1. Operation-level arguments (_sudo=True)
  2. Host data configuration
  3. Group data configuration
  4. Inventory global data
  5. Config object settings
  6. Default values

Source Reference

Location: src/pyinfra/api/config.py:203

Key Classes

  • Config - Main configuration class (line 203)
  • ConfigDefaults - Default values (line 20)

Default Values

All defaults are defined in ConfigDefaults (line 20) and can be viewed in the source.