[英]How to issue commands on remote hosts in parallel using Fabric without using a fabfile?
[英]How to determine fabric role in fabfile to send specific files to hosts
我有一個看起來像這樣的環境:
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()
裝飾器也可以獲取正確的文件列表?
這個問題顯示了一種確定主機是否屬於角色的方法,但是我需要獲取當前正在執行的角色,以便知道要發送的文件列表。
理想的情況是這樣的:
@roles('cisco-collectors', 'brocade-collectors')
def sendFiles():
for file in files[env.current_role]:
put(file)
env.host_string.role
包含最新的fabric
源中的當前角色(未發布)。
在我的測試中,我認為您無法可靠地獲得1.5中的當前角色。 請參閱帕維爾(Pavel)有關此情況將如何變化的答案。 盡管這似乎不方便,但我認為原因是Fabric合並了主機列表,因此角色不會延續。
但是,如果您不需要使用角色功能,則有一個解決方法。 您可以創建一個更改主機列表的任務。 不過,這僅適用於一個角色!
from fabric.api import task, local, env
@task
def role1():
env.current_role = 'role1'
env.hosts.extend(['example.com'])
@task
def role2():
env.current_role = 'role2'
env.hosts.extend(['test.example.com'])
@task
def test():
print env.current_role
print env.hosts
如果現在運行fab role1 test
您將看到:
[localhost] Executing task 'test'
role1
['localhost', 'example.com']
[example.com] Executing task 'test'
role1
['localhost', 'example.com']
不完全是您所追求的……但是可能會幫助您找到可行的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.