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 →
- Prefer a real module.
- Otherwise add
creates:/removes:or an explicitchanged_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.
More on Playbook Design, Handlers and Idempotency
Nginx must reload only when its config file actually changes.BeginnerA task fails halfway and its handler never ran, but the config file was changed. The next run shows no change and the service never reloads.AdvancedYou need three tasks to notify one handler, and want it to run once. Is that automatic?BeginnerYou need to deploy a binary only if it is missing or its checksum differs.IntermediateHow do you safely edit one line in `/etc/ssh/sshd_config` and be sure SSH keeps working?IntermediateA task should run only on Debian-family hosts and a different one on RedHat.Beginner