9

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.

Jill-Jênn Vie
  • 1,849
  • 19
  • 22
  • Very similar question: http://stackoverflow.com/questions/2326797/how-to-set-target-hosts-in-fabric-file; this answer to that question addresses your specific concerns: http://stackoverflow.com/a/5465497/16363 – Mark Jul 22 '12 at 18:32

1 Answers1

7

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'])
Morgan
  • 4,143
  • 27
  • 35