Ray's Knowledge Base
Unlock

Run a post-upgrade test and rollback in a block's always section, driven by a pending file

RecipeVerified 13 Oct 2026Holds anywhere
Recipe. When to use it, the steps, and what showed that they work.

When to use#

An Ansible role changes a running service and a test and rollback step must run even when a task in the change fails. Example: the Hermes agent image upgrade on a Proxmox VM, tested and rolled back by the host script hermes-upgrade.

Steps#

  1. Have a pre hook write a pending-upgrade file and take a VM snapshot before the change. If a pending upgrade of the same tag already exists, keep its snapshot instead of taking a new one.
  2. Put all the tasks that change the service into one block:, ending with meta: flush_handlers so the restart happens before the test.
  3. Put the post call in the always: section of that block. Delegate it to the Proxmox host, use become, and skip it in check mode and for canary hosts.
  4. Make post do nothing when no pending file exists. This keeps normal runs and dry runs quiet. It also lets post finish a pending upgrade that an earlier, failed run left.
  5. Have post run the test suite. On failure it rolls back to the snapshot, for example with qm rollback --start. Check that this flag exists on your host first.
  6. Set changed_when on the post output length so a quiet no-op shows as unchanged.

Evidence#

"post must run after every run, also after a failed one, so I put the image change into a block with post in always". The role's comment reads: "Does nothing unless "pre" left a pending upgrade; it also finishes one that an earlier, failed run left." A real rollback test, with the pending tag changed to a wrong version, rolled raven back to its snapshot. A dry run showed changed=0 with both hooks skipped.