⚡ ~/naveed Interview Prep
⚡ Portfolio Home ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Ansible Automation Interview Questions Scenario 2 of 15 in Ansible Automation
Senior DevOps / SRE [L2] Ansible Idempotency & State Enforcement Production Scenario [L2]
🎯 Target Role / Context: Senior DevOps Scenario #2 · State Enforcement & Idempotency

Q: A production playbook run was aborted midway due to a network timeout. How does Ansible handle partial execution, and how do you guarantee idempotency when re-running?

Ansible is stateless and maintains no persistent backend state file. If a run drops midway, already executed tasks remain in their target state. Re-running the playbook relies on module idempotency to report ok and only apply pending tasks.

#Ansible #Idempotency #Disaster Recovery #Playbooks #Triage
🎙️ Candidate Opening & Architectural Context
""In distributed systems, partial network failures are inevitable. Ansible does not maintain a state database like Terraform state. When a connection drops midway, recovering requires understanding how Ansible verifies host reality and ensuring custom script tasks are designed idempotently.""
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

1️⃣

Understand Ansible's Stateless Execution Model

Unlike Terraform which tracks state in a JSON file, Ansible evaluates state directly on the remote system during each task execution. Tasks already applied before the timeout will evaluate to ok (no changes needed), while incomplete tasks will execute and report changed.

  • No State Lock: There is no lock file preventing an immediate re-run once network connectivity is restored.
  • Host List Filtering: Ansible generates a .retry file (if enabled) listing only the hosts that failed to complete.
2️⃣

Guard Custom Shell/Command Tasks

Declarative modules (package, file, service) are inherently idempotent. However, imperative shell tasks must be safeguarded to prevent duplicate side effects:

  • Use creates: /opt/app/installed.lock to skip execution if artifact already exists.
  • Use removes: /tmp/setup.sh to execute only if cleanup file is present.
  • Use register: result and check return codes before mutating state.
Advertisement
3️⃣

Validate and Re-Execute Playbook

Before full execution, execute in dry-run mode to inspect anticipated delta:

  • ansible-playbook -i inventory site.yml --check --diff
  • Verify that already completed steps report green (ok) and only unfinished steps report yellow (changed).
  • Execute ansible-playbook -i inventory site.yml --limit @site.retry to target only incomplete hosts.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Ansible relies on the target system's current reality rather than a central state lock. Ensure all custom script tasks utilize creates/removes arguments or registered variables to guarantee that re-running after an aborted play never introduces duplicate mutations."
⚡ 60-Second Elevator Pitch Talking Points
  • Ansible is inherently stateless — it queries the managed node's real-time configuration rather than reading a static state file.
  • Tasks completed before the network timeout remain applied; declarative modules (service, file, package) safely report 'ok'.
  • Guard imperative command and shell tasks using 'creates' or 'removes' arguments to prevent duplicate execution.
  • Execute 'ansible-playbook site.yml --check --diff' to verify delta before committing changes to production.
  • Re-run against failed nodes using the auto-generated retry file: '--limit @site.retry'.
  • Implement automated pre-commit linting (ansible-lint) to block non-idempotent module patterns across CI pipelines.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Assuming Ansible has an automated rollback mechanism on task failure Ansible halts execution on failure; it does NOT roll back previous tasks automatically unless custom block/rescue structures are declared.
  2. Using raw shell modules to run echo or append commands without idempotency checks Appends duplicate configuration lines every time the playbook is retried, causing invalid syntax in config files.
  3. Re-running playbooks blindly without running --check --diff first Senior engineers always preview pending mutations during incident recovery to avoid race conditions.
Advertisement
Want more Ansible Automation scenarios?
Explore our complete collection of scenario-based Ansible Automation interview runbooks.
Browse All Ansible Automation Questions →