如何仅在单个主机上 运行 @roles-decorated fabric 任务

How to run a @roles-decorated fabric task on only a single host

我有一个用 @roles 装饰的任务,我偶尔想 运行 在单个主机上(用于金丝雀测试部署)。

from fabric.api import *

env.roledefs = {
    'web-workers': ['django@worker1', 'django@worker2'],
    'some-other-role': ['django@worker2'],
}

@task
@roles('web-workers')
def bogomips():
    run('uptime')

docs for @roles 指出:

...barring an override on the command line, my_func will be executed against the hosts listed [in the role]...

但我无法使这里提到的 "override" 功能发挥作用...我试过了:

$ fab bogomips -H django@worker2
$ fab bogomips -R some-other-role

但它总是在装饰器中提到的整个角色上执行...

我在这里错过了什么?我如何覆盖 @roles 修饰的任务 运行?

根据 Execution model's Order of Precedence,这实际上是预期的行为,在此情况下您必须使用略有不同的语法。

所以这是不起作用的命令:

$ fab bogomips -R some-other-role # fabric ignores the -R values!

这是有效的版本:

$ fab bogomips:roles=some-other-role

这是问题所在:#308: @roles and @hosts decorators ignore command line options

以及文档:http://docs.fabfile.org/en/1.0.0/usage/execution.html#order-of-precedence

  • Per-task, command-line host lists (fab mytask:host=host1) override absolutely everything else.
  • Per-task, decorator-specified host lists (@hosts('host1')) override the env variables.
  • Globally specified host lists set in the fabfile (env.hosts = ['host1']) can override such lists set on the command-line, but only if you’re not careful (or want them to.)
  • Globally specified host lists set on the command-line (--hosts=host1) will initialize the env variables, but that’s it.