When I run this fabfile.py…
from fabric.api import env, run, local, cd
def setenv(foo):
env.hosts = ['myhost']
def mycmd(foo):
setenv(foo)
print(env.hosts)
run('ls')
with this command fab mycmd:bar. I get this output…
['myhost']
No hosts found. Please specify (single) host string for connection:
What, what?! I don’t get it? I’ve set the env.hosts and it seems to be valid “inside” the mycmd function, but for some reason that run command doesn’t know about the hosts I’ve specified.
Color me confused. Any help would be appreciated!
@Chris, the reason you’re seeing this behavior is because the host list is constructed before the task function is called. So, even though you’re changing
env.hostsinside the function, it is too late for it to have any effect.Whereas the command
fab setenv:foo mycmd:bar, would have resulted in something you would have expected:This is the same as the accepted answer, but because of the way
setenvis defined, an argument is needed.Another example:
The output of this is:
As you can see, the host-list is already set to
['other_host', ]when fabric starts to executemycmd.