Python结构嵌套角色

时间:2015-06-21 12:28:48

标签: python fabric

我试图了解如何在结构中使用嵌套roledefs。写了It

  

角色定义不仅仅是主机的配置,而是   可以保留您选择的其他角色特定设置。这是   通过在主机下定义角色作为dicts和主机字符串来实现   键

我的配置就像,

from fabric.api import env
env.roledefs = {
    'prod': {
        'hosts': ['prod1', 'prod2', 'prod3'],
        'db': ['db1']
    },
}

我的问题是如何为特定任务访问db

@roles('db') or @roles('prod')  #<- not sure what to add here
def migrate():
    # migration stuff
    pass

1 个答案:

答案 0 :(得分:1)

我的建议:

roles = {'prod': {'hosts': ['node1', 'node2'], 'db': ['node3']}
         'test': {'hosts': ['test1'], 'db': ['test2']}}

def migrate(role='test'):
    env.roledefs = roles[role]
    execute(_run_prepare)
    execute(_run_migrate)

@roles('hosts', 'db')
def _run_prepare():
    pass # runs on hosts in role "hosts" and hosts in role "db"

@roles('db')
def _run_migrate():
    pass # runs on hosts in role "db"