Rescue & Always
A failing step stops its host: nothing after it runs there. rescue and always change what
happens around that failure. They are blocks of tasks written inside the step, beside its own
tasks:
plan "deploy" { step "Deploy" { shell "touch /tmp/deploy.lock" shell "/opt/app/deploy.sh" rescue { shell "/opt/app/rollback.sh" file "/var/log/deploy-failure.txt" src="templates/failure.txt" template=#true } always { shell "rm -f /tmp/deploy.lock" } } step "Migrate" { shell "/opt/app/migrate.sh" }}rescueruns only if the step failed. If every rescue task succeeds, the failure is handled: the host goes on to the next step — hereMigrate— and ends the run as a success.alwaysruns after the step and any rescue, whether or not they failed — the place for cleanup such as releasing a lock.
A step may have either, both or neither, each at most once. Each needs at least one task,
and takes no attributes: put when= on its tasks instead.
What counts as a failure
Section titled “What counts as a failure”rescue covers the step’s work once it has started:
- a task that fails — a command that exits non-zero, a module error, an undefined variable,
or
register=refusing output that is too long; - its
until=gate timing out; - its
loop=variable being undefined.
An error in the step’s own when= is not rescued: it is decided
before the step starts, and fails the host as before.
At the failure, the step’s tasks stop. The ones after it do not run, and neither do the remaining items of a loop.
Reading the failure
Section titled “Reading the failure”The rescue and always tasks can read two variables that describe it:
| Variable | Holds |
|---|---|
${@error.msg} |
The error, as the run reported it |
${@error.task} |
The task that failed, as module 'resource' — for example shell 'deploy.sh'. Empty when the step failed outside its tasks, in its until= gate or its loop |
They are defined only while a failed step’s rescue and always tasks run, so in always
when="defined ${@error.msg}" tells whether the step failed:
always { shell "rm -f /tmp/deploy.lock" shell "curl -s -X POST https://hooks.example.com/deploy-failed" when="defined ${@error.msg}"}The step’s own tasks, when=, until= and loop= cannot use them — there is no failure yet
— and a plan that tries is rejected when it is parsed. A file template is read only when its
task runs, so a run fails that task instead; glidesh validate
checks the templates too, and reports it before any host is touched.
Secrets in the failure are redacted before a rescue sees it: a decrypted
value in the failed command line or its output reads ***, as it does in every log.
Using them safely
Section titled “Using them safely”Never write ${@error.msg} or ${@error.task} into a shell command. The message holds
the failed command’s output, and the task its interpolated command line: text a host or a
variable controls. Inside a command it is run as shell — no quoting or heredoc is safe
against every value it can hold.
Render them into a file with a file template instead, and let a fixed
command read that file. A template writes each value as text: it is never run, nor expanded
again. The first example above does this, with this templates/failure.txt beside the plan:
${@error.task} failed:${@error.msg}A later task can then pass it on without the text ever reaching a command line:
plan "deploy" { step "Deploy" { shell "/opt/app/deploy.sh" rescue { file "/var/log/deploy-failure.txt" src="templates/failure.txt" template=#true shell "logger -t deploy -f /var/log/deploy-failure.txt" } }}when= is safe too: a condition compares values, and runs nothing.
Undo, then still fail
Section titled “Undo, then still fail”A rescue that succeeds lets the host go on as if the step had worked. When the step’s change must not count as done — a config the service rejected, say — end the rescue with a task that fails. The host stops as before, but only after putting things back:
plan "nginx" { step "Deploy config" { shell "cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.previous" changed-when=#false file "/etc/nginx/nginx.conf" src="files/nginx.conf" template=#true shell "nginx -t" changed-when=#false rescue { shell "mv /etc/nginx/nginx.conf.previous /etc/nginx/nginx.conf" shell "echo 'nginx -t rejected the config; the previous one is back' >&2; exit 1" } always { shell "rm -f /etc/nginx/nginx.conf.previous" changed-when=#false } } step "Reload nginx" subscribe="Deploy config" { systemd "nginx" state="restarted" }}-
A failed rescue fails the host, as the step would have:
alwaysstill runs, then the host stops. The same holds for a failedalways, even when the step itself succeeded. -
Once per step.
rescueandalwaysrun after the step’s loop, not per item, and do not see${@item}: a plan whoserescueoralwaysuses it is rejected when it is parsed, andglidesh validatereports afiletemplate in them that does. -
register=works in both blocks. A rescue that does not run leaves itsregister=variables undefined, as a skipped task does — so a laterwhen="defined ${var}"asks whether the rescue ran:plan "deploy" {step "Deploy" {shell "/opt/app/deploy.sh"rescue {shell "/opt/app/rollback.sh" register="rolled-back"}}step "Report the rollback" when="defined ${rolled-back}" {shell "logger -t deploy 'rolled back'"}} -
Changes count. Tasks in
rescueandalwayscount toward the run’s changed total like any other, and a step whose rescue or always changed something has changed: steps that subscribe to it are triggered. -
A skipped step skips all three. When
when=or tags leave a step out, its rescue and always blocks do not run either. The skipped count includes the step’s own andalwaystasks — the ones a run that succeeds would have run. -
The run shows each block as it starts —
RESCUE step 'Deploy',ALWAYS step 'Deploy'— in the plain output, the TUI and the run log. The failure it handles is still shown and logged above it, but the host’s summary inglidesh logsdoes not report a failure the rescue handled. -
A
hosttask inrescueruns once for all the hosts that reach it, as it does anywhere else.
Under --dry-run
Section titled “Under --dry-run”A preview runs the blocks the same way: rescue if computing the step’s preview failed,
always in any case, each task reporting what it would do. A preview cannot know whether the
real run will fail, so a rescue’s register= variables are neither defined nor undefined
there, and a when= that tests one is reported as
undetermined. For the same reason, when a step’s preview did not fail, an always task’s
when="defined ${@error.msg}" is undetermined too.

