Skip to main content
 首页 » 编程设计

Python 结构嵌套角色

2025年05月04日10qq78292959

我想了解如何在 fabric 中使用嵌套的 roledefs。 It写着,

Role definitions are not necessary configuration of hosts only, but could hold other role specific settings of your choice. This is achieved by defining the roles as dicts and host strings under a hosts key

我的配置是这样的

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 

请您参考如下方法:

我的建议:

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"