Fabric 收到错误时如何继续执行任务
当我定义一个任务在多个远程服务器上运行时,如果该任务在一台服务器上运行并因错误退出,Fabric 将停止并中止该任务。但我想让 Fabric 忽略错误并在下一台服务器上运行任务。我怎样才能让它做到这一点?
例如:
$ 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.
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?
For example:
$ 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自文档:
看起来您可以使用
设置
上下文管理器,如下所示:From the docs:
Looks like you can exercise fine-grained control over where errors are ignored by using the
settings
context manager, something like so:从 Fabric 1.5 开始,有一个 ContextManager 可以使这变得更容易:
更新:我使用以下代码重新确认这在 ipython 中有效。
As of Fabric 1.5, there is a ContextManager that makes this easier:
Update: I re-confirmed that this works in ipython using the following code.
您还可以将整个脚本的 warn_only 设置为 true
You can also set the entire script's warn_only setting to be true with
您应该设置
abort_exception
环境变量并捕获异常。例如:
您也可以将其设置在 with 块中。请参阅此处的文档。
You should set the
abort_exception
environment variable and catch the exception.For example:
You can also set it in a with block. See the documentation here.
在Fabric 2.x中,您只需使用调用的 运行 warn=True 参数。无论如何,调用是Fabric 2.x的依赖项:
在任务中:
In Fabric 2.x you can just use invoke's run with the warn=True argument. Anyway, invoke is a dependency of Fabric 2.x:
From within a task:
至少在 Fabric 1.3.2 中,您可以通过捕获 SystemExit 异常来恢复异常。如果您有多个命令要批量运行(例如部署)并且希望在其中一个命令失败时进行清理,那么这会很有帮助。
In Fabric 1.3.2 at least, you can recover the exception by catching the
SystemExit
exception. That's helpful if you have more than one command to run in a batch (like a deploy) and want to cleanup if one of them fails.就我而言,在 Fabric >= 1.4 这个答案是正确的。
您可以通过添加以下内容来跳过不良主机:
或传递
--skip-bad-hosts
标志/In my case, on Fabric >= 1.4 this answer was the correct one.
You can skip bad hosts by adding this:
Or passing the
--skip-bad-hosts
flag/