Overview
Connectors are pyinfra’s abstraction layer for interfacing with different types of target systems. They handle connection management, command execution, and file transfers across various protocols and platforms.Every host in pyinfra uses a connector to execute commands. The default is SSH, but pyinfra supports local, Docker, and custom connectors.
BaseConnector Class
All connectors inherit fromBaseConnector (defined in src/pyinfra/connectors/base.py):
Key Methods
make_names_data: Static method that generates host entries from connector stringsconnect: Establish connection to targetdisconnect: Close connectionrun_shell_command: Execute shell commandsput_file: Upload filesget_file: Download files
Built-in Connectors
pyinfra includes several built-in connectors:SSH Connector (Default)
The SSH connector uses Paramiko for SSH connections:- Password and key-based authentication
- SSH agent support
- SSH config file support
- Connection retry logic
- SFTP and SCP file transfer
- Port forwarding
Local Connector
Execute commands on the local machine:- Testing deployments
- Configuring the control machine
- Local development
- CI/CD pipelines
Docker Connector
Execute commands inside Docker containers:- Execute commands via
docker exec - File transfer via
docker cp - Run as different users
- Target by container name or ID
Docker-SSH Connector
SSH into Docker containers:Chroot Connector
Execute commands in a chroot environment:- System installation
- Rescue operations
- Container image building
Terraform Connector
Generate inventory from Terraform state:Vagrant Connector
Target Vagrant VMs:Connector Data
Connectors can define typed data classes:Command Execution
Connectors execute commands throughrun_shell_command:
ConnectorArguments
Fromsrc/pyinfra/api/arguments.py:
Example Execution
File Transfer
Upload Files
Download Files
Connection Management
Connecting
Disconnecting
Connection State
Creating Custom Connectors
Basic Custom Connector
Registering Custom Connector
Connector Utilities
CommandOutput
Command Helpers
Best Practices
Use SSH by Default
SSH is secure, well-tested, and works for most use cases.
Local for Testing
Use @local for testing deployments before running on real hosts.
Docker for Containers
Use @docker connector for containerized applications.
Handle Errors
Always handle connection errors and timeouts gracefully.
Reuse Connections
Connectors maintain connections across operations for efficiency.
Custom When Needed
Create custom connectors for proprietary or specialized systems.
Connection Troubleshooting
SSH Connection Issues
Timeout Configuration
Connection Retry
Advanced Examples
Multi-hop SSH
Inventory Generation
Related Concepts
Inventory
Learn how connectors integrate with inventory
Operations
See how operations use connectors to execute commands
State
Understand how State manages connector instances
Host
Learn about the Host-Connector relationship
