Ansible handlers.
A handler reacts to change: it restarts or reloads a service only when a task actually changed something β once, at the end of the play. Watch a changed task drop its handler into the notify queue, the queue dedupe, and the flush fire it exactly once.
Ansible handlers and notifications animated tutorial. Using notify on a task to trigger a handler only when the task reports changed, defining handlers in the play handlers section or a role handlers/main.yml, why a handler runs once at the end of the play in defined order, meta flush_handlers to run queued handlers mid-play, the failure gotcha where queued handlers are skipped and --force-handlers / force_handlers to run them anyway, and listen topics to fan one notify out to many handlers. RHCE EX294 ready.A handler restarts the service only when the config actually changed β once, at the end of the play. Watch a changed task drop its handler into the notify queue, the queue dedupe, and the flush fire it exactly once.
- Β·You can write a basic Ansible play with tasks
- Β·Familiar with the template / copy / service modules
- Β·Understand changed vs ok task results
Use notify and handlers to restart or reload a service only on change, understand that handlers run once at the end of the play in defined order, handle the failure gotcha with --force-handlers, and fan out with listen topics.
pace: 8 minutes
deploy httpd.conf [template]β changedopen firewall [firewalld]β okemptyπ restart httpdβ idleπ reload firewalldβ idleWhy handlers exist
You only want to restart a service when its config actually changed β not on every single run. A HANDLER is a task that runs only when NOTIFIED, and only at the end of the play. Define handlers in the play's `handlers:` section (or, for a role, in roles/NAME/handlers/main.yml). A regular task adds `notify:` pointing at a handler by its exact name.
$tasks:
- template:
src: httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart httpd$handlers:
- name: restart httpd
service:
name: httpd
state: restarted$# name must match EXACTLY
notify: restart httpd β - name: restart httpdA handler is a task that runs only when something actually CHANGED, exactly ONCE, at the END of the play, in the order it is DEFINED. That single rule is what lets you say "restart the service" without restarting it on every single run.
$handlers:$- name: restart httpd β service$notify: restart httpd$notify: [ h1, h2 ]$fires only when task is changed$runs ONCE even if notified many times$runs at END of play$in DEFINED order$- meta: flush_handlers$listen: "topic" + notify: "topic"$--force-handlers / force_handlers: true$not notified β handler skippedWhen does a handler actually run?
Five different tasks all `notify: restart httpd`, and all five report changed. How many times does httpd restart?
Two handlers are both queued. In what order do they run?
A task notifies `restart httpd`, then a later task FAILS before the play ends. What happens to the queued handler?
These aren't graded β they're just for active recall, which is what actually makes the lesson stick.
Ansible roles
Tasks, handlers, templates, and defaults belong together. Package them into a reusable role so the handlers you just learned ride along automatically wherever the role is used.
keep going β these pair well with what you just learned.
Ansible control node & inventory
Ansible is agentless β one control node pushes work to a fleet over SSH. Install ansible-core, write an inventory with groups, ping the fleet, and fire ad-hoc commands. RHCE-ready.
Ansible playbooks & YAML
Plays, tasks, and idempotency. Watch a playbook run task-by-task β ok, changed, skipped β then run it again and watch everything go green with 0 changed. RHCE-ready.
Ansible core modules
file, user, copy, dnf, service β the modules that do the real work. Declare the desired state and let Ansible converge to it, idempotently. RHCE-ready.