slim tabka
slim tabka

Reputation: 43

Fabric run command on different hosts simultaneously

I'm using fabric and I want to download a file simultaneously on different hosts at the same time but when I use

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

I always get 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 

I want to run this from a python code without using the fab command.

Upvotes: 1

Views: 1371

Answers (1)

davidejones
davidejones

Reputation: 1949

I usually use the @parallel decorator (http://docs.fabfile.org/en/1.13/usage/parallel.html) and do something like this.

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

Then i can run fab deploy:prod

Upvotes: 2

Related Questions