Lesson 2 of 9 · 23m
Handlers, Restarts, and the Shape of Safe Change
Use notifications, handler ordering, and service state modeling to avoid unnecessary restarts and partial configuration activation.
Restarting a service is sometimes the correct end of convergence. Restarting it because a template task always reports changed is operational vandalism. Handlers exist so many small configuration mutations can collapse into one deliberate activation step. Used well, they reduce disruption. Used casually, they create a false sense of safety while services reload with broken or half-applied configuration.
The notification contract
A task notifies a handler only when it reports changed:
tasks:
- name: Configure nginx
ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: "0644"
notify: Reload nginx
handlers:
- name: Reload nginx
ansible.builtin.service:
name: nginx
state: reloadedHandlers run at the end of the play by default, even when notified multiple times. That ordering is a feature: package installation, TLS material, and configuration can converge before the service is touched once.
Validate before activation
For critical daemons, activation should depend on validation. Ansible can validate rendered configuration before installing it:
- name: Configure sshd
ansible.builtin.template:
src: sshd_config.j2
dest: /etc/ssh/sshd_config
validate: /usr/sbin/sshd -t -f %s
mode: "0600"
notify: Restart sshdThe %s placeholder is replaced with the candidate file path. If validation fails, the bad file is not installed. Do not skip this for SSH, sudoers, web servers, load balancers, or anything capable of locking you out.
Reload, restart, and reboot are different promises
state: reloaded asks the daemon to reread configuration. state: restarted terminates and starts the process. reboot interrupts the machine. They are not interchangeable:
reload good for nginx, haproxy, sshd when supported
restart required when binaries, linked libraries, or startup flags change
reboot required for kernels, some init systems, and low-level boot changesA restart may be required after package upgrades even if configuration did not change. Model that separately. Do not use configuration notification as the only mechanism for binary activation.
Force handler execution only with intent
meta: flush_handlers runs pending handlers immediately:
- name: Apply firewall rules before dependency checks
ansible.builtin.meta: flush_handlersThis is useful when later tasks need the service already restarted. It is also dangerous because it fragments the activation boundary. Every flush should answer a specific dependency need, not soothe impatience.
Failed hosts still matter
A host that fails before the end of the play may never execute its notified handler. That leaves new configuration on disk and the old process still running. Operational reports must distinguish "task failed" from "configuration staged but not activated." For critical services, use validation, health checks, and explicit post-run verification instead of assuming a green play recap means runtime truth.
Field notes
- Handler names are strings; duplicate names across roles can collide in surprising ways. Keep them specific.
listencan group handler aliases, but indirection makes incident reading harder. Use sparingly.- A service returning
runningdoes not prove it loaded the configuration you just wrote. - Do not notify restarts from tasks that use shell commands and always report changed; fix the task first.
- Rollback means restoring configuration and activating the service again, not merely restoring a file.
Was this lesson helpful?