简体   繁体   English

如何在结构中设置动态主机

[英]how to set dynamic hosts in fabric

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: 但是我只能在shell中运行它,并且该工作将按顺序而不是并行进行:

fab set_hosts mytask

how to run it in fabfile? 如何在fabfile中运行它? so that I can set a decorator @parallel to the job to run parallel. 这样我就可以将装饰器@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_namehost get_namehost; fab set_hosts mytask in shell,input twice. fab set_hosts mytask在shell中,输入两次。 how to define the two jobs into one? 如何将两个工作定义为一个?

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 运行: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 : 在查看fab文档时,您可能需要使用这种方法:

http://docs.fabfile.org/en/1.5/usage/parallel.html 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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM