python结构在所有服务器上从提示符执行操作

2024-10-02 14:15:57 发布

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

我的fab文件上有几个服务器, 我想有一个函数,它会提示我做什么,然后在所有服务器上执行它。你知道吗

def simple():
   actions = prompt('Type the actions: ')
   run(actions)

当我这么做的时候,我得到:

[web1] Type the actions:
        bla bla bla

[web2] Type the actions:
        bla bla bla

[web3] Type the actions:
        bla bla bla

我只想输入一次“bla bla bla”,它将在所有服务器上执行, 有可能吗?你知道吗


Tags: 文件the函数run服务器actionsdeftype
1条回答
网友
1楼 · 发布于 2024-10-02 14:15:57

如果simple()这里是一个Fabric任务,那么这种行为是预期的,因为您将对执行该任务的每个主机进行prompt()调用。我想,您可以使用fab命令行工具,在最简单的情况下:

# fabfile.py

from fabric.api import run, env

env.hosts = ['host1', 'host2']

def simple(command=''):
    run(command)

并按如下方式启动:

$ fab simple:'ls -la'

相关问题 更多 >

    热门问题