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

时间:2017-02-20 16:51:54

标签: python fabric

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

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代码运行它。

1 个答案:

答案 0 :(得分: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