如何在Fabric 2中等待Shell脚本重新启动

时间:2019-05-22 20:29:50

标签: python fabric

我正在使用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)

2 个答案:

答案 0 :(得分:0)

因此,一种解决方法是使用-w标志运行任务,这将启用warn_only并提供所需的功能。不过,最好能够在代码中设置此属性。

答案 1 :(得分:0)

好像您可以对连接类使用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上获得升级文档