首页 > 解决方案 > 如何在 Fabric 2 中等待 shell 脚本重启

问题描述

我正在使用 Fabric 2,并尝试在多个主机上按顺序运行 shell 脚本。在脚本中,它配置了一些设置并重新启动该主机。但是,当我运行我的任务时,它会在脚本在第一台主机上运行后结束(我猜是因为 SSH 连接由于重新启动而终止)。我尝试将 'warn_only' 设置为 True,但我看不到在 Fabric 2 上设置此值的位置。

添加:

with settings(warn_only=True):

引发“NameError:未定义全局名称'settings'”错误。

warn_only 是否有正确的格式?如果在 Fabric 2 中还不可能,是否有办法继续运行我的任务,而不管这次重新启动如何?

我的脚本:

from fabric import *
import time
import csv

@task
def test(ctx):

    hosts = ['1.1.1.1', '2.2.2.2']

    for host in hosts:
        c = Connection(host=host, user="user", connect_kwargs={"password": "password"})

        c.run("./shell_script.sh")

        configured = False

        while not configured:
            result = c.run("cat /etc/hostname")
            if result != "default": configured = True
            time.sleep(10)

标签: pythonfabric

解决方案


看起来您可以使用config连接类的参数而不是with settings()构造,并且warn_only已重命名为warn;

with Connection(host=host, user="user", connect_kwargs={"password": "password"}) as c:
    c.run("./shell_script.sh", warn=True)

更一般地说,您可以在https://www.fabfile.org/upgrading.html#run获取升级文档


推荐阅读