BacklogZero®

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.

Ansible in a Minute — handlers

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: restarted

How 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

Try it with BacklogZero

Ask BacklogZero to generate this automation for your environment.

Get BacklogZero today

Related guides

Back to Ansible in a Minute

Use notify and handlers to run a task after a change · Level Up Labs