如何仅在单个主机上运行@roles装饰的Fabric任务 [英] How to run a @roles-decorated fabric task on only a single host
问题描述
我有一个用@roles
装饰的任务,我有时希望在单个主机上运行(用于金丝雀测试部署).
I have a task decorated with @roles
that I would occasionally like to run on a single host (for canary-testing deploys).
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')
@roles
的文档指出:
...除非在命令行上进行覆盖,否则将对[角色]中列出的主机执行my_func ...
...barring an override on the command line, my_func will be executed against the hosts listed [in the role]...
但是我无法使这里提到的替代"功能正常工作……我已经尝试过:
But I can't get the "override" functionality mentioned here to work... I've tried:
$ fab bogomips -H django@worker2
$ fab bogomips -R some-other-role
但是它总是在装饰器中提到的整个角色上执行...
but it always executes on the entire role mentioned in the decorator...
我在这里想念什么?如何覆盖装饰有@roles
的任务的运行位置?
What am I missing here? How can I override where a @roles
-decorated task is run?
推荐答案
This is actually the intended behavior, according to the Execution model's Order of Precedence, and there's a slightly different syntax that you must use in this scenario.
所以这是不起作用的命令:
So here's the command that doesn't work:
$ fab bogomips -R some-other-role # fabric ignores the -R values!
这是可以工作的版本:
$ fab bogomips:roles=some-other-role
问题出在这里:#308:@roles和@hosts装饰器会忽略命令行选项
和文档: http://docs.fabfile.org/en/1.0.0/usage/execution.html#order-of-precedence
- 每个任务的命令行主机列表(fab mytask:host = host1)绝对覆盖其他所有内容.
- 每个任务,装饰者指定的主机列表(@hosts('host1'))覆盖env变量.
- 在fabfile中设置的全局指定主机列表(env.hosts = ['host1'])可以覆盖在命令行上设置的此类列表,但前提是您不小心(或不希望这样做).
- 在命令行(--hosts = host1)上设置的全局指定的主机列表将初始化env变量,仅此而已.
- 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.
这篇关于如何仅在单个主机上运行@roles装饰的Fabric任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!