Fabric 组 - 字符串的主机列表

Fabric Group - List of hosts to Strings

使用 Fabric2,您可以创建一个组以串行或线程方式执行命令。

g = ThreadingGroup('192.168.101.5', '192.168.101.10', user='username', port=22, connect_kwargs={'password': 'password'})    
g.run('uptime')

# Returns:
# {<Connection host=192.168.101.10 user=esp>: <Result cmd='uptime' exited=0>, <Connection host=192.168.101.5 user=esp>: <Result cmd='uptime' exited=0>}

如果我传入一个列表,我会收到一个 rsplit 错误,因为需要一组字符串。然后我将列表转换为字符串,然后再次 运行:

s = str(servers).strip('[]')
g = ThreadingGroup(s, user='username', port=22, connect_kwargs={'password': 'password'})
g.run('uptime')

# fabric.exceptions.GroupException: {<Connection host='192.168.101.5', '192.168.101.10' user=username>: gaierror(-2, 'Name or service not known')}

在后一个例子中,"Connection host" 被合并,而不是像前一个例子那样分开。我该如何协调?

您不能将列表作为第一个参数传递,那是行不通的。您需要解压列表:

g = ThreadingGroup(*s, user='username', port=22, connect_kwargs={'password': 'password'})