简体   繁体   中英

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.

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM