2017-02-20 71 views
1

我使用的面料,我想在不同的主机在同一时间同时下载一个文件,但是当我使用织物运行指令的同时,

env.hosts = ['192.168.1.2', '192.168.1.3', '192.168.1.4']

我总是得到No hosts found. Please specify (single) host string for connection:

from fabric.api import env , run, sudo, settings 
env.user = 'root' #all the servers have the same username 
env.hosts = ['192.168.1.2', '192.168.1.3', '192.168.1.4'] 
env.key_filename = "~/.ssh/id_rsa" # I have their ssh key 
run('wget file') #The command I need to run in parrallel 

我想在不使用fab命令的情况下从python代码运行此代码。

回答

2

我通常使用@parallel装饰器(http://docs.fabfile.org/en/1.13/usage/parallel.html)并做类似的事情。

env.use_ssh_config = True 
env.user = 'ubuntu' 
env.sudo_user = 'ubuntu' 
env.roledefs = { 
    'uat': ['website_uat'], 
    'prod': ['website01', 'website02'] 
} 

@task 
def deploy(role_from_arg, **kwargs): 
    # on each remote download file 
    execute(download_file, role=role_from_arg, **kwargs) 


@parallel 
def download_file(*args, **kwargs): 
    # some code to download file here 

然后我可以运行fab deploy:prod

+0

感谢它帮我,我用env.hosts代替env.roledefs,我跑了部署()在我的代码没有晶圆厂命令。它的工作方式像魔术一样。 –

+0

不错,很高兴工作! – davidejones