我需要通过打开并读取文件来获取主机来获取结构来设置其主机列表。 以这种方式设置允许我拥有一个庞大的主机列表,而无需每次都为这些数据编辑我的fabfile。
我试过了:
def set_hosts():
env.hosts = [line.split(',') for line in open("host_file")]
def uname():
run("uname -a")
和
def set_hosts():
env.hosts = open('hosts_file', 'r').readlines
def uname():
run("uname -a")
每次尝试使用set_hosts函数时都会出现以下错误:
fab set_hosts uname
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 712, in main
*args, **kwargs
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 264, in execute
my_env['all_hosts'] = task.get_hosts(hosts, roles, exclude_hosts, state.env)
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 74, in get_hosts
return merge(*env_vars)
File "/usr/local/lib/python2.7/dist-packages/fabric/task_utils.py", line 57, in merge
cleaned_hosts = [x.strip() for x in list(hosts) + list(role_hosts)]
AttributeError: 'builtin_function_or_method' object has no attribute 'strip'
答案 0 :(得分:15)
您在这里遇到的问题是您将env.hosts设置为函数对象,而不是列表或可迭代。你需要在readlines之后的parens来实际调用它:
def set_hosts():
env.hosts = open('hosts_file', 'r').readlines()