同时在不同主机上执行 Fabric 运行 命令

Fabric run command on different hosts simultaneously

我正在使用结构,我想同时在不同的主机上同时下载一个文件,但是当我使用

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 

我想从 python 代码中 运行 这个,而不使用 fab 命令。

我通常使用@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