0Pricing
Ansible Academy · Lesson

Defining Handlers & Restart Patterns

Write reusable restart-service handlers.

Defining Handlers & Restart Patterns is a free Ansible Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Where Handlers Live

Handlers go in their own handlers section of a play, separate from your normal tasks. That keeps reactive actions clearly grouped.

tasks:
  - name: Deploy config
    ansible.builtin.template:
      src: app.conf.j2
      dest: /etc/app.conf
    notify: Restart app
handlers:
  - name: Restart app
    ansible.builtin.service:
      name: app
      state: restarted

A Handler Is a Task

A handler uses the exact same syntax as a task: a name and a module call. The only difference is that it waits to be notified.

Name Is the Contract

The handler's name is the string other tasks reference in their notify. Treat it as a stable, exact label you will reuse.

Restart with service

A classic restart handler uses the service module with state set to restarted. It stops and starts the unit in one step.

- name: Restart nginx
  ansible.builtin.service:
    name: nginx
    state: restarted

Reload vs Restart

Prefer reloaded when a service supports it. A reload re-reads config without dropping connections, unlike a full restart.

- name: Reload nginx
  ansible.builtin.service:
    name: nginx
    state: reloaded

Use systemd Directly

The systemd module gives finer control on systemd hosts, including daemon_reload after you change a unit file.

- name: Restart app
  ansible.builtin.systemd:
    name: app
    state: restarted
    daemon_reload: true

Keep Handlers Generic

Write one reusable Restart nginx handler and notify it from many tasks. Avoid copying the same restart logic everywhere.

Handlers Run in Definition Order

When several handlers are queued, they run in the order they are defined, not the order they were notified. Plan that sequence.

Order Matters for Dependencies

If a reload must happen before a restart, define them in that order in the handlers list so the queue plays out correctly.

Handlers in Roles

In a role, handlers live in handlers/main.yml. Tasks in the role notify them by name, exactly like in a plain playbook.

Naming for Clarity

Use action-style names like Restart nginx or Reload sshd. Clear names make output readable and notify lines obvious.

Quick Check

You changed a config and want the running service to pick it up without dropping connections. Which state is best?

Recap

You can now define handlers in the handlers section, choose restart vs reload, and rely on definition-order execution. 🎯

Frequently asked questions

Is the “Defining Handlers & Restart Patterns” lesson free?

Yes — the full text of “Defining Handlers & Restart Patterns” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

What will I learn in “Defining Handlers & Restart Patterns”?

Write reusable restart-service handlers. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ansible Academy?

No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Defining Handlers & Restart Patterns” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ansible Academy lesson?

Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Notify a Handler on Change
  2. Defining Handlers & Restart Patterns
  3. When Handlers Run & flush_handlers
  4. Listen: One Trigger, Many Handlers
← Back to Ansible Academy