How to continue a task when Fabric receives an error

后端 未结 7 1500
迷失自我
迷失自我 2020-12-04 23:16

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

7条回答
  •  遥遥无期
    2020-12-04 23:44

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

    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
    

提交回复
热议问题