将不同的参数传递给Fabric中的不同主机 [英] Passing different arguments to different hosts in Fabric

查看:132
本文介绍了将不同的参数传递给Fabric中的不同主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,以在多个主机上启动负载生成实验.我可以编写一个bash脚本来启动多个ssh会话,但是我希望使用更结构化的东西.由于我在大多数脚本编写过程中都使用Python,因此我认为 Fabric 看起来是个不错的选择.

I am writing a script to launch a load generation experiment on several hosts. I could write a bash script to start multiple ssh sessions, but I was hoping to use something more structured. Since I use Python for most of my scripting, I thought Fabric looked like a good option.

唯一的问题是我需要在每个命令中传递少量的主机特定数据(实际上只是一个ID或计数器),并且我想并行运行它们.

The only problem is that I need to pass a small amount of host specific data with each command (really just an id or counter), and I would like to run them in parallel.

换句话说,我想执行以下操作,其中每个主机的host_num不同(可能只是增加).

In other words, I would like to do something like the following, where host_num is different (possibly just incremented) for each host.

@parallel
def launch():
    with cd('/working/dir'):
        run("./start/script -id=%d", host_num)

在Fabric中这可能吗?如果没有,我还有其他工具可以用来完成相同的事情吗?

Is this possible in Fabric? If not, is there another tool I could use to accomplish the same thing?

推荐答案

您可以检查用户/主机.每个任务都知道他们当前在其中运行的环境:

You could check against user / host. Each task knows about environment they currently run in:

env.hosts = ['user@host1.com', 'user@host2.com']

@task
def test():
    print '%(user)s@%(host)s' % (env)

    if env.host == 'host1.com':
        id = 1
    elif ...

    run('echo "%s"' % (id))

可以用一种更加优雅的方式随意编写它:)(一个建议是字典,用于id查找的情况类似于case语句)

Feel free to write it in more elegant way :) (one suggestion being dictionaries used similar to case statements for the id lookup)

这篇关于将不同的参数传递给Fabric中的不同主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆