Ansible:在任务失败时执行清理 [英] Ansible: Perform Cleanup on Task Failure
问题描述
我目前正在编写一个遵循这种通用格式并通过 cron 作业运行的 Ansible play:
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: yes
应用于 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: yes
,如果任务遇到致命错误,整个游戏仍然会立即失败,所以我必须还运行 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屋!