What is Idempotency?
An operation is idempotent if running it multiple times has the same effect as running it once:vim is already installed and skips execution, making zero changes.
Why Idempotency Matters
Idempotent operations provide several critical benefits:- Safe to re-run - Deploy scripts can run repeatedly without breaking things
- Predictable results - Same code always produces same end state
- Change detection - See exactly what will change before applying
- Drift correction - Re-running deploys fixes configuration drift
- Faster execution - Skip operations that don’t need changes
How pyinfra Achieves Idempotency
Pyinfra achieves idempotency through a two-phase process:Phase 1: Detect Changes
pyinfra first checks the current state using facts:- Checks if
/etc/myapp/config.txtexists - Checks file permissions, owner, and group
- Compares with desired state (mode=644, user=root)
- Only generates commands if changes are needed
Phase 2: Apply Changes
If changes are detected, pyinfra generates and executes the minimum commands needed:Idempotent vs Non-Idempotent Operations
Idempotent Operations
Most pyinfra operations are idempotent by default:Non-Idempotent Operations
Some operations are inherently non-idempotent and run every time:Operations marked with
is_idempotent=False will execute every time. Use them carefully in production deploys.Dry-Run Mode
Dry-run mode shows you what will change without making any changes:Smart Service Restarts
Use operation return values to restart services only when configs change:Checking Operation Results
Operation return values provide metadata about execution:Available Methods
Handling Non-Idempotent Commands
When you must use non-idempotent shell commands, add checks to make them idempotent:Check Before Execute
Use Conditional Execution
Working with Facts
Facts are how pyinfra gathers information about the current state:Common Facts
Facts are cached during the prepare phase. They represent the state before operations are executed.
Caution: Facts During Execution
Be careful when using facts that may change during execution:Always prefer using idempotent operations over manual fact checks. Operations handle state detection correctly.
File Upload Idempotency
File operations detect changes by comparing checksums:- Calculates checksum of local file
- Calculates checksum of remote file (if exists)
- Only uploads if checksums differ or file doesn’t exist
Template Idempotency
Templates also use checksums to detect changes:- Renders template with provided variables
- Calculates checksum of rendered content
- Compares with remote file checksum
- Only uploads if different
Testing Idempotency
Always test that your deploys are truly idempotent:1
Run deploy first time
2
Run deploy second time
3
Verify no changes
The second run should report:
Best Practices
Next Steps
- Parallel Execution - Scale idempotent operations across many hosts
- Using Operations - Learn about all available idempotent operations
- Deploying Applications - Build complete idempotent deployments
