BacklogZero®

Ansible in a Minute · Ansible Core ·

Use changed_when so a task can notify a handler

Short answer

Handlers run only when a task reports changed. Use changed_when to control whether notify fires.

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

copy httpd.conf, mark the task changed, and notify Restart apache

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 apache when the copy reports changed
  hosts: all
  become: true
  gather_facts: false
  tasks:
    - name: Copy httpd configuration
      ansible.builtin.copy:
        src: ./new_httpd.conf
        dest: /etc/httpd/conf/httpd.conf
      changed_when: true
      notify: Restart apache

  handlers:
    - name: Restart apache
      ansible.builtin.service:
        name: httpd
        state: restarted

How it works

notify queues a handler only when the task reports changed. changed_when true forces a changed result so the handler runs after this copy. Use a real condition when change depends on command output.

When to use it

  • Forcing a handler after a task that would otherwise report ok
  • Pairing command modules with notify

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-09-17

Notes

changed_when: true always notifies. Prefer a condition such as "'updated' in result.stdout" when the module already returns useful output. See Defining changed for more changed_when examples.

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 changed_when so a task can notify a handler · Level Up Labs