Ansible in a Minute · Ansible Core ·
Use notify and handlers to run a task after a change
Short answer
Use notify with a handler so Ansible restarts a service only after a config file changes.
Request
ansible restart service only after config file changes
Example
A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.
YAML
---
- name: Restart service only after config file changes
hosts: all
become: true
tasks:
- name: Deploy application config
ansible.builtin.template:
src: app.conf.j2
dest: /etc/myapp/app.conf
mode: "0644"
notify: Restart myapp
handlers:
- name: Restart myapp
ansible.builtin.service:
name: myapp
state: restartedHow it works
notify queues a handler by name. Ansible runs notified handlers once at the end of the play when at least one notifying task reports changed.
When to use it
- Restarting a service only after a real config change
- Avoiding unnecessary restarts on idempotent runs
Requirements
- Ansible: >=2.14
Validation
- Syntax validated
- Not execution-tested
- Last verified 2026-09-09
Notes
Handler names must match the notify string exactly. Pair handlers with accurate changed_when when you use command modules.
Reference
- Handlers (Ansible docs)
Try it with BacklogZero
Ask BacklogZero to generate this automation for your environment.