0

the code below can set the dynamic hosts:

def set_hosts():
    env.hosts = ['host1', 'host2']

def mytask():
    run('ls /var/www')

but I only can run this in shell,and the job will work sequential not in parallel:

fab set_hosts mytask

how to run it in fabfile? so that I can set a decorator @parallel to the job to run parallel.

def set_namehost():
    env.hosts = ['namehost']

def get_namehost():
    run('ls /var/www')

def set_hosts():
    env.hosts = ['host1', 'host2']

def mytask():
    run('ls /var/www')

I only could run: fab set_namehost get_namehost ; fab set_hosts mytask in shell,input twice. how to define the two jobs into one?

kuafu
  • 1,466
  • 5
  • 17
  • 28

1 Answers1

0

If i understand you correctly your are trying to run a task in multiple hosts.

If thats the case you can do it this way

from fabric.api import *

env.roledefs = {
    'host1'   : ['you@yourFirstHost.com'],
    'host2'   : ['you@yourSecondHost.com'],
    'host3'   : ['you@yourThirdHost.com']
}

@task
def runTask():
    for subtask in (deploy_host1, deploy_host2, deploy_host3):
        execute(subtask)


@roles('host1')
def deploy_host1();
    run('ls /var/www')

@roles('host2')
def deploy_host2();
    run('ls /var/www')


@roles('host3')
def deploy_host3();
    run('ls /var/www')

runing : fab runTask

will run the defined taks in all the hosts. i would suggest to go for this approach especially when commands you want to run on your hosts are different.

While looking at the fab docs you might want to use this approach :

http://docs.fabfile.org/en/1.5/usage/parallel.html

from fabric.api import *

@parallel
def runTask():
    run('ls /var/www')

fab -H host1, host2, host3 runTask

Hope this helps

Redian
  • 334
  • 1
  • 7