当我定义一个要在多个远程服务器上运行的任务时,如果该任务在一个服务器上运行并退出并出现错误,Fabric将停止并中止该任务.但是我想让Fabric忽略该错误并在下一台服务器上运行该任务.我怎样才能做到这一点?
When I define a task to run on several remote servers, if the task runs on server one and exits with an error, Fabric will stop and abort the task. But I want to make fabric ignore the error and run the task on the next server. How can I make it do this?
例如:
$ fab site1_service_gw [site1rpt1] Executing task 'site1_service_gw' [site1fep1] run: echo 'Nm123!@#' | sudo -S route [site1fep1] err: [site1fep1] err: We trust you have received the usual lecture from the local System [site1fep1] err: Administrator. It usually boils down to these three things: [site1fep1] err: [site1fep1] err: #1) Respect the privacy of others. [site1fep1] err: #2) Think before you type. [site1fep1] err: #3) With great power comes great responsibility. [site1fep1] err: root's password: [site1fep1] err: sudo: route: command not found Fatal error: run() encountered an error (return code 1) while executing 'echo 'Nm123!@#' | sudo -S route ' Aborting.推荐答案
来自文档:
... Fabric默认为快速失败"行为模式:如果出现任何错误,例如远程程序返回非零返回值或fabfile的Python代码遇到异常,执行将立即停止.
... Fabric defaults to a "fail-fast" behavior pattern: if anything goes wrong, such as a remote program returning a nonzero return value or your fabfile’s Python code encountering an exception, execution will halt immediately.
这通常是所需的行为,但是该规则有很多例外,因此Fabric提供了布尔值env.warn_only.默认为False,这意味着错误情况将导致程序立即中止.但是,如果在发生故障时将env.warn_only设置为True(例如使用设置上下文管理器),则Fabric将发出警告消息,但会继续执行.
This is typically the desired behavior, but there are many exceptions to the rule, so Fabric provides env.warn_only, a Boolean setting. It defaults to False, meaning an error condition will result in the program aborting immediately. However, if env.warn_only is set to True at the time of failure – with, say, the settings context manager – Fabric will emit a warning message but continue executing.
看起来您可以通过使用 settings上下文管理器,如下所示:
Looks like you can exercise fine-grained control over where errors are ignored by using the settings context manager, something like so:
from fabric.api import settings sudo('mkdir tmp') # can't fail with settings(warn_only=True): sudo('touch tmp/test') # can fail sudo('rm tmp') # can't fail更多推荐
Fabric收到错误时如何继续执行任务
发布评论