Python Fabric as a Library, execute and environments?

120 views Asked by At

I've tried really hard to find this but no luck - I'm sure it's possible I just can't find and example or figure out the syntax for myself

  • I want to use fabric as a library
  • I want 2 sets of hosts
  • I want to reuse the same functions for these different sets of hosts (and so cannot us the @roles decorator on said functions)

So I think I need:

from fabric.api import execute, run, env

NODES = ['192.168.56.141','192.168.56.152']
env.roledefs = {'head':['localhost'], 'nodes':NODES}
env.user('r4space')

def testfunc ():
    run('touch ./fred.txt')

execute(testfunc(),<somehow specific 'head' or 'nodes' as my hosts list and user >) 

I've tried a whole range of syntax // hosts=NODES, -H NODES, user='r4space'....much more but I either get a syntax error or "host_string = raw_input("No hosts found. Please specify (single)""

If it makes a difference, ultimately my function defs would be in a separate file that I import into main where hosts etc are defined and execute is called.

Thanks for any help!

1

There are 1 answers

0
charli On

You have some errors in your code.

  • env.user('r4space') is wrong. Should be env.user = 'r4space'
  • When you use execute, the first parameter should be a callable. You have used the return value of the function testfunc.

I think if you fix the last line, it will work:

execute(testfunc, hosts = NODES)