How to continue the task when Fabric receives an error message

When I define a task to run on multiple remote servers, if the task runs on the same server and fails, Fabric will stop and abort the task. But I want the fabric to 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 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. 
+91
python fabric
Oct 06 '10 at 21:11
source share
7 answers

From the docs :

... Fabric by default uses the "fast-fast" behavior pattern: if something goes wrong, such as a remote program returning a non-zero return value, or your Python fabfiles code that encounters an exception, execution will immediately stop.

This is usually the desired behavior, but there are many exceptions to the rule, so Fabric provides the boolean parameter env.warn_only. The default value is False, which means that an error condition will lead to an immediate interruption of the program. However, if env.warn_only is set to True during a crash - for example, using the settings context manager - Fabric will display a warning message, but continue to run.

It looks like you can exercise detailed control over where errors are ignored using the settings context manager , something like this:

 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 
+143
Oct 6 2018-10-10
source share

As with Fabric 1.5, there is a ContextManager that makes this simpler:

 from fabric.api import sudo, warn_only with warn_only(): sudo('mkdir foo') 

Update: I re-confirmed that this works in ipython using the following code.

 from fabric.api import local, warn_only #aborted with SystemExit after 'bad command' local('bad command'); local('bad command 2') #executes both commands, printing errors for each with warn_only(): local('bad command'); local('bad command 2') 
+30
Oct 23 '13 at 15:30
source share

You can also set the entire script parameter warn_only to true with

 def local(): env.warn_only = True 
+13
Nov 09 '11 at 20:21
source share

You must set the environment variable abort_exception and catch the exception.

For example:

 from fabric.api import env from fabric.operations import sudo class FabricException(Exception): pass env.abort_exception = FabricException # ... set up the rest of the environment... try: sudo('reboot') except FabricException: pass # This is expected, we can continue. 

You can also install it in a block with a block. See the documentation here .

+10
Jan 16 '15 at 18:15
source share

In Fabric 1.3.2, at least you can throw an exception by SystemExit exception. This is useful if you have several commands to run in a package (for example, to deploy) and want to clear if one of them does not work.

+7
Dec 01 '11 at 19:13
source share

In Fabric 2.x, you can simply use invoke run with the warn = True argument. In any case, invoke is a dependency of Fabric 2.x:

 from invoke import run run('bad command', warn=True) 

From inside the task:

 from invoke import task @task def my_task(c): c.run('bad command', warn=True) 
+6
Jun 20 '18 at 8:37
source share

In my case, on Fabric> = 1.4 this answer was correct.

You can skip failed hosts by adding the following:

 env.skip_bad_hosts = True 

Or pass the flag --skip-bad-hosts /

-four
Sep 19 '16 at 14:25
source share



All Articles