有没有办法让execute
面料尊重装饰者(除了host
, hosts
,role
和--- 见这里roles
) ,或者其他方式来完成类似的事情)?这是一个用例:exclude_hosts
from fabric.api import task, execute, run, runs_once
@task
def setup_environment():
# ... set up env.hosts, env.roledefs, env.use_ssh_config, etc.
@task
def provision():
# ... do some stuff on each host here, e.g. install mongodb
@task
def is_primary():
return run('mongo --quiet --eval "db.isMaster().ismaster"') == 'true'
@task
@runs_once
def change_to_primary():
env.hosts = []
for host, result in execute(is_primary, roles=('mongo',)).iteritems():
if result:
env.hosts.append(host)
@task
def add_user():
# ... do something here that needs to be done on primary
如果我从命令行运行以下任务序列,这很好:
> fab setup_environment provision change_to_primary add_user
但由于我总是运行change_to_primary
并且add_user
作为配置的一部分,我想修改provision
以便我可以运行fab setup_environment provision
、拥有set_primary
和add_user
执行,如下所示:
@task
def provision():
# ... do some stuff on each host here, e.g. install mongodb
execute(change_to_primary)
execute(add_user)
但是,这会执行change_to_primary
很多次(不运行一次),这与命令行用法不同。有没有办法做到这一点?