Skip to main content
The State class manages the state of a pyinfra deployment, tracking hosts, operations, configuration, and execution progress.

Creating a State

Create a state object with an inventory and configuration:

Constructor

Inventory
The inventory containing target hosts.
Config
Configuration object. If None, uses defaults.
bool
default:true
Whether to check if operations will make changes during the prepare phase.

Properties

Inventory
The inventory containing all hosts.
Config
The configuration object for this deployment.
Pool
Gevent pool for parallel execution.
StateStage
Current execution stage (Setup, Connect, Prepare, Execute, Disconnect).
bool
Whether operations are currently being executed.
set[Host]
Set of all hosts that have been activated.
set[Host]
Set of currently active hosts (excludes failed hosts).
set[Host]
Set of hosts that have failed.
dict[str, StateOperationMeta]
Dictionary mapping operation hashes to operation metadata.
dict[Host, dict[str, StateOperationHostData]]
Dictionary mapping hosts to their operation data.

State Stages

The state progresses through these stages:

Set Stage

Host Management

Activate Host

Mark a host as active:

Fail Hosts

Mark hosts as failed:
This automatically:
  • Removes hosts from active_hosts
  • Adds hosts to failed_hosts
  • Checks against FAIL_PERCENT threshold
  • Raises PyinfraError if threshold exceeded

Check Host Limit

Check if a host is within the current limit:

Operation Management

Get Operation Order

Get the topologically sorted order of operations:

Get Operation Metadata

Get Operation Data for Host

Set Operation Data for Host

Results Tracking

Get Host Metadata

Get Host Results

Callbacks

Register callback handlers to respond to events:

Available Callbacks

Host Callbacks:
  • host_before_connect(state, host) - Before connecting
  • host_connect(state, host) - After successful connection
  • host_connect_error(state, host, error) - On connection error
  • host_disconnect(state, host) - After disconnection
Operation Callbacks:
  • operation_start(state, op_hash) - Operation starting
  • operation_host_start(state, host, op_hash) - Operation starting on host
  • operation_host_success(state, host, op_hash, retry_count) - Operation succeeded
  • operation_host_error(state, host, op_hash, retry_count, max_retries) - Operation failed
  • operation_host_retry(state, host, op_hash, retry_num, max_retries) - Operation retrying
  • operation_end(state, op_hash) - Operation completed

Trigger Callbacks

Warning Counter

Track warnings per stage:

Change Detection

Complete Example

Here’s a complete example using the State API:

Source Reference

Location: src/pyinfra/api/state.py:145

Key Classes

  • State - Main state class (line 145)
  • StateStage - Execution stages enum (line 93)
  • StateOperationMeta - Operation metadata (line 106)
  • StateOperationHostData - Per-host operation data (line 119)
  • StateHostMeta - Per-host metadata (line 127)
  • StateHostResults - Per-host results (line 137)
  • BaseStateCallback - Callback base class (line 41)

Key Methods

  • __init__() - Initialize state (line 187)
  • init() - Complete initialization (line 206)
  • set_stage() - Set execution stage (line 284)
  • activate_host() - Activate host (line 370)
  • fail_hosts() - Mark hosts as failed (line 382)
  • get_op_order() - Get operation order (line 310)
  • add_callback_handler() - Add callbacks (line 298)