Interview Prep/Ansible/Playbook Design, Handlers and Idempotency

Nginx must reload only when its config file actually changes.

BeginnerScenarioPlaybook Design, Handlers and Idempotency
Answer

Situation read → Classic handler use case.

What I'd do →

tasks:
  - ansible.builtin.template:
      src: nginx.conf.j2
      dest: /etc/nginx/nginx.conf
      validate: nginx -t -c %s
    notify: Reload nginx
handlers:
  - name: Reload nginx
    ansible.builtin.service: { name: nginx, state: reloaded }

Why → Handlers run once at the end of the play, only if a notifying task reported changed; validate stops a broken file from being installed.

Interview tip → Use meta: flush_handlers if later tasks depend on the reload.

All Ansible questions Roadmap: Ansible

More on Playbook Design, Handlers and Idempotency