Python Fabric - 未找到主机

标签 python fabric

我不知道我做错了什么。这似乎是一件简单的事情,应该可行。我可以单独运行任务“test_task”并且它可以工作。如果我将它作为“deploy_test”函数的一部分运行,那么它会提示我: 未找到主机。请指定用于连接的(单个)主机字符串:

env.roledefs = {
    'test_servers': ['testserver1.domain.com', 'testserver2.domain.com']
}

@roles("test_servers")
def test_task():
    env.user = "test_user"
    sudo("sh /usr/bin/something", user="other_user")

def deploy_test():
    test_task()  

最佳答案

装饰器仅在您从 fab 命令执行任务时才起作用:

fab test_task

如果您想使用任务deploy_test,您有很多选择:

1.- 将 test_task 作为任务执行,而不是作为函数执行。如果您还有其他任务需要在不同主机列表上的 deploy_test() 上执行,则可以采用这种方法:

def deploy_test():
   execute(test_task)  

2.- 将角色装饰器放在 deploy_test() 上:

@roles("test_servers")
def deploy_test():
   test_task()

请记住,Python 函数与结构任务不同。 Fabric 任务会在后台执行一些操作,例如设置主机列表、用户,并且如果您调用 Python 函数,则会保留此状态。

关于Python Fabric - 未找到主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32766660/

相关文章:

在VMware Windows机器上Python smtp连接总是失败

python - 使用 ssh 配置文件和 RSA key 连接到多个主机时出现结构错误

python - 从 Python 检查 HDFS 中是否存在文件

python - 构造方法异常

python - 我的单词计数程序只计算文本文件中的最后一个单词。我哪里做错了?

python - 将数组添加到 Pandas 数据框

python - 带有 PyDev + Eclipse : "include-ids" option no longer allowed, 的 PyLint 1.0.0 破坏了 Eclipse 集成

python - Fab 未找到主机 key

python - Fabric 3 询问 sudo 密码一次

javascript - 查找函数参数的数量及其名称