gpt4 book ai didi

python - Fabric收到错误时如何继续任务

转载 作者:IT老高 更新时间:2023-10-28 21:09:11 26 4
gpt4 key购买 nike

当我定义一个任务在多个远程服务器上运行时,如果该任务在一个服务器上运行并出现错误退出,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.

最佳答案

来自 the docs :

... 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.

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 context manager 对忽略错误的位置进行细粒度控制。 ,类似这样:

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

关于python - Fabric收到错误时如何继续任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com