Interview Prep/Ansible/Modules, Idempotency and Handlers

A handler named `restart {{ app_service }}` is never found when notified as `restart myapp`

AdvancedTroubleshootingModules, Idempotency and Handlers
Answer

Likely causes → Handler names are templated once, when handlers are loaded, using only the variables available at that point (play vars, role defaults and vars, extra vars). If app_service comes from inventory, set_fact or include_vars, the name does not render the way you expect, and the notify does not match.

Fix → Give the handler a static name, template the module arguments instead, and use listen:

- name: Restart application
  ansible.builtin.systemd:
    name: "{{ app_service }}"
    state: restarted
  listen: app restart needed

Prevent → Never template handler names or notify strings. Keep names static and parameters dynamic.

All Ansible questions Roadmap: Ansible

More on Modules, Idempotency and Handlers