Jill-Jênn Vie
Jill-Jênn Vie

Reputation: 1841

Fabric env.hosts and run in the same method => No hosts found

Why:

from fabric.api import env, run

def update():
    env.hosts = ['apycat']
    run('cd /var/www/menu; svn up')

does not work when I fab update, while:

from fabric.api import env, run

env.hosts = ['apycat']

def update():
    run('cd /var/www/menu; svn up')

does?

Didn't find anything about this in the docs.

Upvotes: 9

Views: 2669

Answers (1)

Morgan
Morgan

Reputation: 4131

Specifying the host list after the fab command has already made the host list for the fab task will not work. So for the first example you have the update task doesn't have a host list set, to then allow the following run() to operate over. A good section in the docs for this is here.

But it shuold also be noted you can get a use case like the first to work in one of two way. First being with the settings() context manager:

def foo():
    with settings(host_string='apycat'):
        run(...)

The other being with the newer api function execute():

def bar():
    run(...)

def foo():
    execute(bar, hosts=['apycat'])

Upvotes: 7

Related Questions