Debug Mode
Enable debug mode for verbose output:Dry Run Mode
Test operations without executing commands:Verbose Output
Print Command Input and Output
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: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
Best Practices
- Use —dry first: Always test with —dry before executing
- Enable debug mode: Use —debug when troubleshooting
- Check facts early: Verify fact values at the start of operations
- Log liberally: Add logger statements in complex operations
- Test incrementally: Test operations on a single host first
- Use assertions: Add assert statements to catch unexpected state
- Check return values: Always inspect operation results
- Handle errors: Use try/except in operations and facts
- Version control: Keep deployment scripts in git for rollback
- Document issues: Maintain a log of common issues and solutions
Debug Checklist
When debugging issues:- Run with
--dry --debugto 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
- Learn about Performance Tuning to optimize deployments
- Explore Writing Operations for better operation design
- See Writing Facts for custom fact debugging
- Check API Reference for complete API documentation
