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

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

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

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

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