← RHCE in Ansible: automation and operations
05 / 23 · 130 MIN

Handlers, failures, and batches

Encode the meaning of change, failure, recovery, and stopping.

Concept and mechanism

A task can succeed without changing state; another can change files without producing a usable service. Define failed_when and changed_when from the operation contract. A failed_when condition list uses implicit conjunction: if failure is required when any condition occurs, express the intended disjunction.

For textual flags, normalize using an appropriate conversion instead of assuming the string false is always interpreted as Boolean false. Handlers are notified by changes and normally wait for defined boundaries. If a check depends on loaded configuration, ensure pending handlers have run before that check.

Guided application

Blocks with rescue enable recovery but do not automatically catch every problem. Definition errors and unreachable hosts do not follow the same rescue path as an ordinary failed task. Successful rescue can permit continuation, including avoiding failure thresholds someone expected to trigger.

In a fictional rollout, rollback works but acceptance policy requires stopping after functional failure. Encode that decision explicitly. Serial controls host batches; forks controls concurrent execution capacity and is not equivalent to accepted batches.

Include functional checking and reentry before advancing. The team should distinguish service recovery, change acceptance, and authorization to continue expansion.

Read the notification sequence

The lab uses TRACE messages to represent operations. It reloads no services and checks no real applications. It starts with two changed tasks notifying activate on one host: the trace shows both writes and one activation.

When a flush_handlers follows the first write and another changed write occurs later, the handler executes twice, once per phase. Therefore define where activation should occur rather than assuming one execution for the entire play. Another experiment notifies second before first, but handlers defined as first then second execute in definition order.

When a check depends on earlier activation, document that dependency and rehearse the observed sequence. A listen topic can notify multiple handlers; the original release-ready example triggers activation and checking under distinct names. Traces demonstrate ordering, not service health.

Control activation scope

A task loops over alpha and beta; only alpha reports change. With notify:activate-{{ item }}, the experiment observes both handlers. Aggregate task change does not necessarily match intended activation scope per component.

If only alpha is authorized, separate tasks or control notifications through appropriate results and conditions, then rehearse again. Another experiment starts with activation_allowed=true, notifies the handler and changes the variable to false before execution.

The handler condition uses the later value and activation is skipped. Notification does not freeze initial authorization. Finally, --skip-tags activation did not prevent a notified handler carrying that tag.

Do not use tag exclusion as authorization control for that path. The contract should specify which operations may be notified and under which conditions they may execute.

Remove collisions and retain observed discrepancies

The first attempt created two definitions named activate and expected the last one based on current documentation. Installed core 2.19.0 executed the first definition in the fixture file. That discrepancy was reproduced and retained; the narrative was not changed to pretend the expected operation occurred.

The correction assigns distinct activate-old and activate-new names and explicitly notifies activate-new, producing the intended trace. Installed code was inspected: handler search reverses handler blocks but traverses tasks inside each block in existing order.

That reading helps investigate the discrepancy without establishing a universal rule across versions and loading arrangements. For production handover, remove the collision and retain version, file and exercised sequence. Human review should distinguish the intended operation from the operation actually observed.

Distinguish failure, recovery and final action

A changed task notifies activate and the next task fails. Without forced handlers, no activation trace appears. With force_handlers:true, the trace appears but the process still exits 2.

Running a pending operation neither proves configuration is safe to activate nor removes earlier failure. In the rescue experiment, ansible_failed_task.name and ansible_failed_result.msg identify rejected validation. If rescue itself fails, always still runs on this ordinary failure path, after does not run and exit remains failed.

With ignore_errors:true on the original task, flow continues inside the block, passes through always and never enters rescue. Decide which failures require recovery and which may be ignored; trace counts do not replace that decision. Controller loss and unreachable hosts were not simulated in this lab and need separate handling.

Rehearse stopping criteria

With two local aliases, end_host conditional on worker_a lets worker_b continue. End_play, executed when worker_a meets the condition, ends the play for both. Both commands can finish without failure: zero exit does not prove final tasks executed.

For batch limits, inventory contains five aliases and serial is 4. With max_fail_percentage=25, one failure among four permits worker_e to enter; two failures prevent entry. An additional experiment with threshold 20 and one failure also blocks worker_e, distinguishing the batch denominator from the total of five hosts.

The percentage must be exceeded. Translate business policy into these criteria and test boundary cases. If policy requires stopping at the first rejection, a limit allowing exactly 25% does not implement it.

Guided change-window exercise

Before running the runner, write each fixture’s expected sequence and final exit code. Distinguish absent traces, skipped tasks, failed tasks and handler execution. Both final runs use core 2.19.0 and pass 26 checks across 20 invocations each.

The five aliases share a controller; they are not five isolated servers. Next, prepare acceptance criteria for a fictional batch change: prerequisite, configuration, activation, functional evidence, recovery and expansion rule. Identify which failure prevents the next batch and which evidence proves stopping.

Introduce another notification after a flush and a rescue failure to test your prediction. Summarize learning for RUN: unique names, explicit order, current conditions, tested limits and change outcome distinguished from recovery. Connect these criteria with inventory, delegation and actual service rehearsals.

IN PRACTICE

Successful rollback recovers the previous service; it does not show the new version was accepted.

Common pitfalls

List as OR; rescue as universal catch; forks as serial; recovery as approval.

Related topics: State and repeatability · Inventories, variables, and configuration · Access and execution environment

Take this idea with you

Rehearsing notifications and failures demonstrates flow; accepting change still requires actual service state and expansion policy.

Create account

References

Red Hat®, RHCE and Ansible are trademarks or registered trademarks of Red Hat, Inc. bigsavant.com is an independent preparation platform and is not affiliated with, associated with, sponsored, authorised or endorsed by Red Hat. Content and questions are original, are not official exam questions, and completing our tests does not award or guarantee any certification. Names are used only to identify the subject. All other trademarks belong to their respective owners.