This is related to my previous question, but a different one.
I have the following fabfile:
from fabric.api import *
host1 = '192.168.200.181'
offline_host2 = '192.168.200.199'
host3 = '192.168.200.183'
env.hosts = [host1, offline_host2, host3]
env.warn_only = True
def df_h():
with settings(warn_only=True):
run("df -h | grep sda3")
And the output is:
[192.168.200.199] run: df -h | grep sda3
Fatal error: Low level socket error connecting to host 192.168.200.199: No route to host
Aborting.
After the execution hits the offline server, it aborts immediately, regardless of the other servers in the env.hosts list.
I have used the env setting "warn_only=True", but maybe I'm using it improperly.
How can I modify this behavior so that it will only prints the error and continue executing?
As of version 1.4 Fabric has a
--skip-bad-hosts
option that can be set from the command line, or by setting the variable in your fab file.Documentation for the option is here: http://docs.fabfile.org/en/latest/usage/fab.html#cmdoption--skip-bad-hosts
Don't forget to explicitly set the timeout value also.
Based on Matthew's answer, I came up with a decorator that accomplishes just that:
It is especially useful when you have multiple hosts and roles.
According to the Fabric documentation on warn_only,
This will not help in the case of a server being down, since the failure occurs during the SSH attempt before executing
run
/sudo
/local
.One solution would be to create a function to check if each server is up prior to executing your tasks. Below is the code that I used.
You're not using it improperly. You can even just provide
--warn-only=true
on the command line. It's the documented method suggested by the development team.