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

标签 python fabric

我有一个用 @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]...

但是我无法让此处提到的“覆盖”功能起作用......我已经尝试过:

$ 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.

关于python - 如何仅在单个主机上运行 @roles-decorated 结构任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41291940/

相关文章:

python - Fab 未找到主机 key

python - 停止 Fabric 任务运行

pip - Oracle Linux 6.5 上的 Fabric 失败并显示 "pkg_resources.DistributionNotFound: paramiko>=1.10"

python - 如何在 anaconda 的 IPython 中更改 Python 版本

javascript - 将多个 JSON 文件中的键和值附加到一个 JSON 对象

python - 如何在for循环中读取这些元组并转换int类型?

python - fabfile.py 不工作 : No module named Crypto

python - 当我的任务继承自 Task 类时,如何使用 @roles 装饰 Fabric 任务?

python - 无法在 Mac 上安装 Twisted 17.1.0

python - 是否可以使用 python 执行参数敏感性分析?