fabric在两个不同的服务器上运行两个不同命令的正确方法是什么?

2024-04-26 03:43:50 发布

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

假设我有两个带相关参数的命令:

  • command1
  • command2

我想在ServerA上运行第一个命令,在ServerB上运行第二个命令

在fabric中通过改变env是正确的方法吗?你知道吗

给予还是索取,下面的方法正确吗?我不能让主机列表包含所有服务器,然后选择告诉run方法在哪个服务器上执行吗?你知道吗

env.hosts = ['ServerA']
run( command1 )
env.hosts = ['ServerB']
run( command2 )

谢谢!你知道吗


Tags: 方法run命令服务器env列表参数fabric
1条回答
网友
1楼 · 发布于 2024-04-26 03:43:50

考虑到结构的限制,显然最好的方法是使用execute方法,而不是run。你知道吗

from fabric.tasks import execute

def execute_task(command,
                 host):                  
    """
        wrapper around the execute task of fabric. 
        It runs the command remotely via ssh. 
    """    
    return execute(command, host=host)

如果ssh密码是相同的,那么它将很好地工作,因为您可以在环境中设置env.password,并且不会要求您输入密码。你知道吗

相关问题 更多 >