Ansible Tutorial · Playbooks · Lesson 8 of 20

Ansible Handlers

Restart services only when configuration changes.

Members can save this page, take notes and track progress. See membership · Sign in

What is a handler?

A handler is a task that runs only when notified by another task. The classic use case: restart a service only if its config file actually changed.

How handlers work

  1. A task includes notify: <handler name>.
  2. If that task reports changed, the handler is queued.
  3. All handlers run at the end of the play, after all tasks finish.
  4. If a task reports ok (no change), the handler is not triggered.

Basic example

---
- name: Deploy nginx config
  hosts: webservers
  become: true

  tasks:
    - name: Copy nginx config
      ansible.builtin.template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: restart nginx

    - name: Copy app config
      ansible.builtin.copy:
        src: files/app.conf
        dest: /etc/app/app.conf
      notify: reload app

  handlers:
    - name: restart nginx
      ansible.builtin.service:
        name: nginx
        state: restarted

    - name: reload app
      ansible.builtin.service:
        name: app
        state: reloaded

If nginx.conf.j2 has not changed since the last run, the template task reports ok and the handler is never called. This avoids unnecessary restarts.

Multiple notifications

A handler runs at most once per play, even if notified by multiple tasks:

  tasks:
    - name: Update nginx.conf
      ansible.builtin.template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: restart nginx

    - name: Update default site
      ansible.builtin.template:
        src: default.j2
        dest: /etc/nginx/sites-enabled/default
      notify: restart nginx

Both tasks notify the same handler, but nginx restarts only once at the end of the play.

listen

The listen keyword lets multiple handlers respond to a single notification topic:

  handlers:
    - name: restart nginx
      ansible.builtin.service:
        name: nginx
        state: restarted
      listen: web config changed

    - name: clear cache
      ansible.builtin.file:
        path: /var/cache/nginx
        state: absent
      listen: web config changed

A task that calls notify: web config changed triggers both handlers.

flush_handlers

Normally handlers run at the end of a play. If you need a handler to run immediately (for example, restart a database before the next task inserts data), use meta: flush_handlers:

  tasks:
    - name: Update database config
      ansible.builtin.template:
        src: postgresql.conf.j2
        dest: /etc/postgresql/16/main/postgresql.conf
      notify: restart postgresql

    - name: Run handlers now
      ansible.builtin.meta: flush_handlers

    - name: Create application database
      community.postgresql.postgresql_db:
        name: myapp
Handler triggered on change
cat > /tmp/handler_demo.yml << 'EOF'
---
- name: Handler demo
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Create a file (simulates config change)
      ansible.builtin.copy:
        content: "new config"
        dest: /tmp/ansible_handler_test.conf
      notify: show notification

  handlers:
    - name: show notification
      ansible.builtin.debug:
        msg: "Handler was triggered because the config changed!"
EOF
ansible-playbook /tmp/handler_demo.yml

Output

PLAY [Handler demo] ***********************************************************

TASK [Create a file (simulates config change)] ********************************
changed: [localhost]

RUNNING HANDLER [show notification] *******************************************
ok: [localhost] => {
    "msg": "Handler was triggered because the config changed!"
}

PLAY RECAP ********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Run this on your computer — see “Get Started” for setup.

Run again — handler is NOT triggered (no change)
ansible-playbook /tmp/handler_demo.yml

Output

PLAY [Handler demo] ***********************************************************

TASK [Create a file (simulates config change)] ********************************
ok: [localhost]

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Run this on your computer — see “Get Started” for setup.

Test yourself

When do handlers run by default?

  1. Immediately after the notifying task
  2. At the end of the play
  3. At the end of the playbook
  4. Before the next task
Show answer

Answer: At the end of the play

Handlers are queued and run at the end of the play (after all tasks), not immediately. Use meta: flush_handlers to trigger them earlier.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake