Python 从外部主机文件设置结构主机列表
我需要让fabric通过打开并读取一个文件来设置其主机列表以获取主机。 这样设置可以让我拥有一个庞大的主机列表,而无需每次为这些数据编辑我的文件 我试过这个:Python 从外部主机文件设置结构主机列表,python,fabric,Python,Fabric,我需要让fabric通过打开并读取一个文件来设置其主机列表以获取主机。 这样设置可以让我拥有一个庞大的主机列表,而无需每次为这些数据编辑我的文件 我试过这个: def set_hosts(): env.hosts = [line.split(',') for line in open("host_file")] def uname(): run("uname -a") 及 每次尝试使用函数集_hosts时,我都会遇到以下错误: fab set_hosts uname Trac
def set_hosts():
env.hosts = [line.split(',') for line in open("host_file")]
def uname():
run("uname -a")
及
每次尝试使用函数集_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'
这里遇到的问题是将env.hosts设置为函数对象,而不是列表或iterable。您需要readlines后面的parens,才能实际调用它:
def set_hosts():
env.hosts = open('hosts_file', 'r').readlines()
def set_hosts():
env.hosts = open('hosts_file', 'r').readlines()