Ansible:对任务失败执行清理 [英] Ansible: Perform Cleanup on Task Failure
问题描述
我目前正在按照这种通用格式编写Ansible剧本,并通过cron作业运行:
I'm currently writing an Ansible play that follows this general format and is run via a cron job:
pre_tasks:
-Configuration / package installation
tasks:
-Work with installed packages
post_tasks:
-Cleanup / uninstall packages
上述问题是有时 tasks
部分中的命令失败,而 post_tasks
部分不运行时,系统处于凌乱的状态.即使发生或致命错误,是否可以强制 post_tasks
中的命令运行?
The problem with the above is that sometimes a command in the tasks
section fails, and when it does the post_tasks
section doesn't run, leaving the system in a messy state. Is it possible to force the commands in post_tasks
to run even if a failure or fatal error occurs?
我当前的方法是将 ignore_errors:是
应用于 tasks
部分下的所有内容,然后将 when:
有条件地应用于每个任务单独检查先前的命令是否成功.
My current approach is to apply ignore_errors: yes
to everything under the tasks
section, and then apply a when:
conditional to each task to individually check if the prior command succeeded.
此解决方案似乎像黑客一样,但由于即使设置了 ignore_errors:是
,它也会变得更糟,如果在执行任务时遇到致命错误,则整个游戏仍然会立即失败,因此我必须还可以执行cron'd bash脚本,以在执行播放后手动检查事物.
This solution seems like a hack, but it gets worse because even with ignore_errors: yes
set, if a Fatal error is encountered for a task the entire play will still immediately fail, so I have to also run a cron'd bash script to manually check on things after reach play execution.
我要保证的是,即使 tasks
失败, post_tasks
仍然可以运行.我确定有一种方法可以不使用bash脚本包装器.
All I want is a guarantee that even if tasks
fails, post_tasks
will still run. I'm sure there is a way to do this without resorting to bash script wrappers.
推荐答案
不要使用 post_tasks
块,而是将清理过程作为常规 tasks
的一部分.
Don't use post_tasks
block but rather have your cleanup process as part of regular tasks
.
这篇关于Ansible:对任务失败执行清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!