2017-02-20 72 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