结构 2.5,主机不可用如何跳过?



我使用的是 Fabric2.5,服务器不多(10-20 台(,任何时候,每个人都可能变得无法访问,这是正常行为。如何检查连接或跳过错误的主机?

在 Fabric1.4中,我们可以使用--skip-bad-hosts(和env.skip_bad_hosts(来绕过有问题的主机,但此功能现在无法在 Fabric2.5中移植。

我以前没有在这个美妙的框架上做过任何事情,也许我做错了什么? 我的fabfile.py

import os
from fabric.connection import Connection
from fabric.tasks import task
from fabric.config import Config
USERNAME = os.getenv('USERNAME')
PASSWORD = os.getenv('PASSWORD')
def get_instance_ips():
return ['10.10.10.10', '10.10.10.11', '10.10.10.12', '10.10.10.12', ... ]
def get_hosts():
ips = sorted(get_instance_ips())
return [{
"host": f"{USERNAME}@{ip}",
"connect_kwargs": {"password": PASSWORD},
} for ip in ips]
@task
def firstTask(c):
print("CONNECTION....")
for index, host in enumerate(get_hosts()):
print(f"****** Run in host {index} at {host['host']} ******")
remote = Connection(**host)
remote.run('some action')

现在,如果任何地址不可用,则会出现错误:

...
File "/usr/local/lib/python3.8/site-packages/paramiko/client.py", line 349, in <lambda>
retry_on_signal(lambda: sock.connect(addr))
socket.timeout: timed out

我可以返回到 Fabric1.4版本,但我想使用 Fabric2.5,感谢您的任何帮助。

我不知道这是否是最好的解决方案,但我们可以使用 try/除了连接

hosts = ["admin@web1", "admin@web2"]
for host in hosts:
try:
conn = Connection(host)
some_function(conn)
except Exception as e:
print("ERROR: ", e)

最新更新