简体   繁体   English

Python Fabric - 找不到主机

[英]Python Fabric - No hosts found

I don't know what I'm doing wrong. 我不知道我做错了什么。 It seems like a simple thing that should work. 这似乎是一件应该有效的简单事情。 I can run the task "test_task" by itself and it works. 我可以自己运行任务“test_task”并且它可以工作。 If i run it as part of the "deploy_test" function then it prompts me with: No hosts found. 如果我作为“deploy_test”函数的一部分运行它,它会提示我: 找不到主机。 Please specify (single) host string for connection: 请指定(单个)主机字符串以进行连接:

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()  

The decorator only works if you execute the task from fab command: 只有从fab命令执行任务时,装饰器才有效:

fab test_task

If you want to use the task deploy_test , you have many options: 如果要使用任务deploy_test ,则有许多选项:

1.- Execute test_task as a task, not as function. 1.-将test_task作为任务执行,而不是作为函数执行。 This is the way to go if you have other tasks that should be executed on deploy_test() on a different host list: 如果您有其他任务应该在另一个主机列表上的deploy_test()上执行,那么这是deploy_test()

def deploy_test():
   execute(test_task)  

2.- Put the role decorator on deploy_test() : 2.-将角色装饰器放在deploy_test()

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

Keep in mind that a python function is different from a fabric task. 请记住,python函数与fabric任务不同。 A fabric task do some stuff under the hood, like setting the host list, the user, and this state is keeped if you call python functions. 一个结构任务做一些事情,比如设置主机列表,用户,如果你调用python函数,这个状态就会保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM