我尝试在fabric中循环所有服务器配置,并获得如下主机名:
from fabric.api import env, run
def serv_foo():
env.hosts = ['bla@example.com']
def serv_bar():
env.hosts = ['bla@example2.com']
def get_all_servers():
return {(k,v) for k,v in globals().items() if k.startswith("serv_")}
def get_hostnames():
for serv_name, serv_fptr in get_all_servers():
print(env.hosts)
serv_fptr()
print(env.hosts)
hostname = run("hostname")
print(hostname)
但是,这样做失败了:
>>> fab get_hostnames
>>> []
>>> ['bla@example.com']
>>> No hosts found. Please specify (single) host string for connection:
如何在结构中动态更新主机?你知道吗
fab serv_foo get_hostnames
。对此不感兴趣。你知道吗execute(serv_fptr)
给出了相同的问题。你知道吗
应该使用execute为
run
命令设置主机:或settings上下文管理器:
希望有帮助。你知道吗
相关问题 更多 >
编程相关推荐