Automatic rollback when a play fails half way — collection prototype, looking for feedback

Hi all,

When a deploy fails after it has already changed a host (new config written, service restarted, then the health check fails), the host is left half-changed. Today the fix is hand-written undo tasks in rescue:, which drift out of sync with the forward tasks.

I built a small collection that does the undo automatically for the things you list:

- role: raphgm.transaction.atomic

vars:

atomic_paths: [/etc/nginx/conf.d/app.conf]

atomic_packages: [nginx]

atomic_services: [nginx]

atomic_tasks: deploy.yml

atomic_verify: healthcheck.yml

If a task or the health check fails, it restores the files (and removes new ones), the package versions and the service state, then restarts services on the restored config. The journal is kept on the managed host, so a rollback still works if the controller dies mid-run.

Repo, with a 30-second localhost demo: GitHub - raphgm/ansible-collection-transaction: Ansible collection: snapshot files, packages and services, and roll back automatically when a play fails. · GitHub CI runs sanity on ansible-core 2.18, 2.19 and devel, plus a real test where a broken nginx config is deployed and rolled back.

Limits: it does not undo command/shell side effects such as DB migrations, and package downgrades need the old version to still be available. Details are in the README.

This replaces my earlier core proposal (ansible/proposals#223). I closed that PR because I’d rather prove the idea as a collection first.

Two questions:

  1. Is this useful to you, and what would you need it to restore that it doesn’t today?
  2. Next step is recording state automatically from the tasks themselves, so you don’t have to list paths. Is an action plugin the right place for that, or is there a small core hook that would be better?

Thanks for reading.