Skip to main content
Debugging infrastructure deployments can be challenging. This guide provides techniques and tools to identify and fix issues in your pyinfra deployments.

Debug Mode

Enable debug mode for verbose output:
In Python:

Dry Run Mode

Test operations without executing commands:
In code:

Verbose Output

Per-operation control:

Inspect Operation Results

Check operation execution details:

Logging

Custom Logging

Save Output to File

Debugging Facts

Inspect Fact Values

Test Facts Interactively

Use pyinfra shell:
Create a fact testing script:
Run it:

Debugging Operations

Check Operation State

Trace Operation Execution

Debugging Connection Issues

Test SSH Connectivity

SSH Configuration Issues

Connector-Specific Debugging

Common Issues and Solutions

Issue: Operation Fails Silently

Problem: Operation doesn’t execute or fails without error messages. Solution: Enable debug mode and check operation conditions:

Issue: Fact Returns Unexpected Value

Problem: Fact returns None or wrong value. Solution: Debug the fact command and processing:

Issue: Commands Not Idempotent

Problem: Operations make changes every time they run. Solution: Add proper state checks:

Issue: Slow Performance

Problem: Deployment takes too long. Solution: Profile and optimize (see Performance Tuning):

Interactive Debugging

Using Python Debugger

Using IPython

Testing Operations

Unit Test Operations

Integration Tests

Run:

Best Practices

  1. Use —dry first: Always test with —dry before executing
  2. Enable debug mode: Use —debug when troubleshooting
  3. Check facts early: Verify fact values at the start of operations
  4. Log liberally: Add logger statements in complex operations
  5. Test incrementally: Test operations on a single host first
  6. Use assertions: Add assert statements to catch unexpected state
  7. Check return values: Always inspect operation results
  8. Handle errors: Use try/except in operations and facts
  9. Version control: Keep deployment scripts in git for rollback
  10. Document issues: Maintain a log of common issues and solutions

Debug Checklist

When debugging issues:
  • Run with --dry --debug to see what would happen
  • Check SSH connectivity independently
  • Verify inventory is loaded correctly
  • Test facts return expected values
  • Check operation conditions (_if, _when)
  • Inspect operation results (did_succeed, stdout, stderr)
  • Review logs for errors and warnings
  • Test on single host before deploying to all
  • Verify file permissions and ownership
  • Check for conflicting operations

Next Steps