Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting fabric hosts list from an external hosts file

Tags:

python

fabric

I need to get fabric to set its hosts list by opening and reading a file to get the hosts. Setting it this way allows me to have a huge list of hosts without needing to edit my fabfile for this data each time.

I tried this:

def set_hosts():
    env.hosts = [line.split(',') for line in open("host_file")]

def uname():
    run("uname -a")

and

def set_hosts():
    env.hosts = open('hosts_file', 'r').readlines

def uname():
    run("uname -a")

I get the following error each time I try to use the function 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'
like image 761
siesta Avatar asked Dec 06 '22 15:12

siesta


1 Answers

The problem you're hitting here is that you're setting env.hosts to a function object, not a list or iterable. You need the parens after readlines, to actually call it:

def set_hosts():
    env.hosts = open('hosts_file', 'r').readlines()
like image 118
spinlok Avatar answered Dec 09 '22 14:12

spinlok