Interview Prep/Ansible/Playbook Design, Handlers and Idempotency

Your playbook reports `changed` on every run for a task that runs a shell command. How do you fix it?

BeginnerScenarioPlaybook Design, Handlers and Idempotency
Answer

Situation read → command and shell always report changed because Ansible cannot know the outcome.

What I'd do →

  1. Prefer a real module.
  2. Otherwise add creates:/removes: or an explicit changed_when.
- ansible.builtin.command: /opt/app/bin/migrate
  register: mig
  changed_when: "'Applied' in mig.stdout"

Why → Honest change reporting is what makes idempotency checks and handlers trustworthy.

Interview tip → A second run of a healthy playbook should show changed=0.

All Ansible questions Roadmap: Ansible

More on Playbook Design, Handlers and Idempotency