如何使用fabric在具有不同参数的多个主机上并行运行同一任务?

2024-04-25 06:43:01 发布

您现在位置:Python中文网/ 问答频道 /正文

你知道吗演示.py你知道吗

from fabric.api import env, run,execute

env.hosts = ['10.1.1.100','10.1.1.200']
env.remotePath = {'10.1.1.100':'/home','10.1.1.200':'/var'}
env.parallel=True

def mytask(remotePath):
    run('ls %s' % remotePath)

def test():
    execute(mytask,env.remotePath[env.host])

制造-f演示.py试验

我想使用@parallel decorator并行执行10.1.1.100的ls /home命令和10.1.1.200的ls /var命令,有什么方法可以实现吗?你知道吗


Tags: runfrompy命令envapihomeexecute
1条回答
网友
1楼 · 发布于 2024-04-25 06:43:01

使用host_string获取当前主机,然后获取要使用的命令/参数。你知道吗

@parallel
def mytask():
  host_ip = env.host_string
  remote_path = env.remotePath[host_ip]
  run('ls %s' % remote_path)

根据Fabric的API文档:host_string

Defines the current user/host/port which Fabric will connect to when executing run, put and so forth. This is set by fab when iterating over a previously set host list, and may also be manually set when using Fabric as a library.

希望这有帮助:)

相关问题 更多 >