Skip to main content
The local connector executes operations on the local machine using subprocesses. This is useful for managing the machine you’re running pyinfra from.

Overview

The @local connector runs commands on your local machine instead of connecting to remote hosts via SSH.
The local connector is only compatible with macOS and Linux hosts. Windows is not supported.

Basic Usage

CLI Examples

Inventory

You can only have one @local host in your inventory. Multiple @local entries will raise an InventoryError.

Example Deploy

Here’s a complete example managing the local machine:

Use Cases

Local Development Setup

Bootstrap a development environment:

CI/CD Pipelines

Use in CI/CD to configure the build agent:

Testing Operations

Test operations locally before deploying remotely:

Combining Local and Remote Hosts

You can mix local and remote hosts in the same inventory:
Use host data to differentiate:

Privilege Escalation

Use sudo for operations requiring elevated privileges:

API Usage

Use the local connector in API mode:

Command Execution

The local connector uses subprocess to execute commands:

Environment Variables

Pass environment variables to local commands:

Timeout

Set command timeouts:

Limitations

The local connector has some limitations:
  • Only one @local host per inventory
  • Not compatible with Windows
  • No connection pooling (each command is a new subprocess)
  • File operations copy files unnecessarily (source and dest are same filesystem)

Security Considerations

When using @local, be careful with:
  • File permissions (operations run as current user)
  • Sudo access (may require password prompts)
  • Destructive operations (directly affect your machine)
  • User input (risk of command injection)
Always test with --dry flag first:

Comparison with Other Connectors

Source Reference

Location: src/pyinfra/connectors/local.py:26

Key Properties

  • handles_execution - This connector handles command execution directly

Key Methods

  • run_shell_command() - Execute command locally (line 50)
  • put_file() - Copy file locally (line 107)
  • get_file() - Copy file locally (line 132)