擅长:python、mysql、java
<p><a href="http://docs.fabfile.org/en/1.4.1/usage/execution.html#failure-handling" rel="nofollow noreferrer"><strong>Fabric Failure handling</strong></a></p>
<p>构建任务列表后,Fabric将按照<a href="http://docs.fabfile.org/en/1.4.1/usage/execution.html#execution-strategy" rel="nofollow noreferrer">Execution strategy</a>中的说明开始执行它们,直到所有任务都在它们的整个主机列表上运行。但是,Fabric默认为<strong>“fail fast”</strong>行为模式:<em>如果出现任何错误,例如远程程序返回非零返回值或fabfile的Python代码遇到异常,则执行将立即停止。</em></p>
<p>这通常是所需的行为,但是规则有许多异常,因此Fabric提供了一个布尔设置<code>env.warn_only</code>。它默认为<code>False</code>,这意味着错误条件将导致程序立即中止。但是,如果在失败时将<code>env.warn_only</code>设置为<code>True</code>,那么<a href="http://docs.fabfile.org/en/1.4.1/api/core/context_managers.html#fabric.context_managers.settings" rel="nofollow noreferrer">settings</a>上下文管理器结构将发出警告消息,但仍将继续执行。</p>
<pre><code>def my_task():
with settings(
hide('warnings', 'running', 'stdout', 'stderr'),
warn_only=True
):
if run('ls /etc/lsb-release'):
return 'Ubuntu'
elif run('ls /etc/redhat-release'):
return 'RedHat'
</code></pre>