python fabric no host found must manually set 'env.host_string' -
is there way work env.hosts? opposed having loop manually whenever have multiple hosts run on?
i trying use fabric api, not have use inconvenient , kludgey fabric command line call. set env.hosts variable in 1 module/class , call class instance method run fabric command. in called class instance can print out env.hosts list. yet when try run command tells me can't find host.
if loop through env.hosts array , manually set env.host variable each host in env.hosts array, can run command work. odd set env.user variable in calling class , is picked up.
e.g. works:
def uptest(self): print('env.hosts = ' + str(env.hosts)) host in env.hosts: env.host_string = host print('env.host_string = ' + env.host_string) run("uptime")
output this:
env.hosts = ['ec2-....amazonaws.com'] env.host_string = ec2-....amazonaws.com [ec2-....amazonaws.com] run: uptime [ec2-....amazonaws.com] out: 18:21:15 2 days, 2:13, 1 user, load average: 0.00, 0.01, 0.05 [ec2-....amazonaws.com] out:
this doesn't work... work if run "fab" file... makes no sense me.
def uptest(self): print('env.hosts = ' + str(env.hosts)) run("uptime")
this output:
no hosts found. please specify (single) host string connection:
i did try putting @task decorator on method (and removing 'self' reference since decorator didn't that). no help.
is there way work env.hosts? opposed having loop manually whenever have multiple hosts run on?
finally, fixed problem using execute() , exec.
main.py
#!/usr/bin/env python demo import fabricsupport hosts = ['localhost'] myfab = fabricsupport() myfab.execute("df",hosts)
demo.py
#!/usr/bin/env python 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
Comments
Post a Comment