Skip to main content
 首页 » 编程设计

python之如何确定 fabfile 中的结构角色以将特定文件发送到主机

2025年05月04日11findumars

我有一个看起来像这样的环境:

env.roledefs = { 
    'cisco-collectors': ['hosta', 'hostb', 'hostc'], 
    'brocade-collectors': ['hosta', 'hostd'] 
} 

我有一些特定文件需要发送给特定角色的主机:

files = { 
    'cisco-collectors': ['/path/to/filea', '/path/to/fileb'], 
    'brocade-collectors': ['/path/to/filec', '/path/to/filed'] 
} 

如何编写我的 sendFiles() 函数,以便在命令行上指定角色时,甚至使用 @roles() 装饰器时,我将能够获得正确的文件名单?

This question显示了一种确定主机是否属于某个角色的方法,但我需要获取当前正在执行的角色,以便我知道要发送哪个文件列表。

理想情况下它应该是这样的:

@roles('cisco-collectors', 'brocade-collectors')     
def sendFiles(): 
  for file in files[env.current_role]: 
    put(file) 

请您参考如下方法:

env.host_string.role 包含最新 fabric 源(未发布)中的当前角色。

The commit