Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python fabric no host found must manually set 'env.host_string'

Tags:

python

env

fabric

Is there any way to get this to work with env.hosts? As opposed to having to loop manually whenever I have multiple hosts to run this on?

I am trying to use the fabric api, to not have to use the very inconvenient and kludgey fabric command line call. I set the env.hosts variable in one module/class and then call a another class instance method to run a fabric command. In the called class instance I can print out the env.hosts list. Yet when I try to run a command it tells me it can't find a host.

If I loop through the env.hosts array and manually set the env.host variable for each host in the env.hosts array, I can get the run command to work. What is odd is that I also set the env.user variable in the calling class and it is picked up.

e.g. this works:

    def upTest(self):
        print('env.hosts = ' + str(env.hosts))
        for host in env.hosts:
            env.host_string = host
            print('env.host_string = ' + env.host_string)
            run("uptime")

output from this:

env.hosts = ['ec2-....amazonaws.com']
env.host_string = ec2-....amazonaws.com
[ec2-....amazonaws.com] run: uptime
[ec2-....amazonaws.com] out:  18:21:15 up 2 days,  2:13,  1 user,  load average: 0.00, 0.01, 0.05
[ec2-....amazonaws.com] out:

This doesn't work... but it does work if you run it from a "fab" file... makes no sense to me.

    def upTest(self):
        print('env.hosts = ' + str(env.hosts))
        run("uptime")

This is the output:

No hosts found. Please specify (single) host string for connection: 

I did try putting an @task decorator on the method (and removing the 'self' reference since the decorator didn't like that). But to no help.

Is there any way to get this to work with env.hosts? As opposed to having to loop manually whenever I have multiple hosts to run this on?

like image 511
Bill Rosmus Avatar asked Apr 05 '13 18:04

Bill Rosmus


1 Answers

Finally, I fixed this problem by using execute() and exec.

main.py

#!/usr/bin/env python

from demo import FabricSupport

hosts = ['localhost']

myfab = FabricSupport()
myfab.execute("df",hosts)

demo.py

#!/usr/bin/env python

from fabric.api import env, run, execute

class FabricSupport:
    def __init__(self):
        pass

    def hostname(self):
        run("hostname")

    def df(self):
        run("df -h")

    def execute(self,task,hosts):
        get_task = "task = self.%s" % task
        exec get_task
        execute(task,hosts=hosts)

python main.py

[localhost] Executing task 'hostname'
[localhost] run: hostname
[localhost] out: heydevops-workspace
like image 101
mcsrainbow Avatar answered Oct 05 '22 23:10

mcsrainbow