简体   繁体   English

如何为每个任务隔离结构中的环境主机的用户输入

[英]How to isolate user input for env.hosts in fabric per task

I have a few tasks in a fabric script. 我在Fabric脚本中有一些任务。 I'm trying to figure out how one would allow for the setting of the env.hosts or the @hosts decorator to be isolated to a given task. 我试图弄清楚如何允许将env.hosts或@hosts装饰器的设置隔离到给定任务。 I want to have some tasks in my fab file to have preset hosts while I could feed a file that could be parsed as a tuple of hosts to others. 我想在fab文件中执行一些任务以拥有预设的主机,同时我可以将一个可以解析为主机元组的文件提供给其他人。 I would also like to have that file determined at run time. 我还希望在运行时确定该文件。

I have this: 我有这个:

def host_list():
    host_file = raw_input("enter the file containing the list of hosts: ")
    host_list = open(host_file, 'r')
    host_list = host_list.read().strip('\n')
    host_list = host_list.split(',')

    return host_list

I have a task: 我有一个任务:

@task
def hostname():
    run('hostname')

I can get env.hosts set properly when I have the host_list function separated into commands, but I have other tasks I don't want to have fabric prompt to set env.hosts. 当将host_list函数分成命令时,可以正确设置env.hosts,但是我还有其他任务,我不想让结构提示设置env.hosts。 I tried adding the steps inside the task functions, but I get prompted with every iteration. 我尝试在任务函数中添加步骤,但是每次迭代都会提示我。 I tried to feed the #hosts decorator with the host_list function, but it gave me an error about the function object not being iterable. 我试图用host_list函数提供#hosts装饰器,但是它给我一个关于函数对象不可迭代的错误。 Is there a way to isolate the host_list function to only certain tasks? 有没有一种方法可以将host_list函数仅隔离到某些任务?

There are plenty of questions in stack overflow with this answer. 这个答案在堆栈溢出中有很多问题。 But to give you an idea, you could send the file as an argument to host_list(), and then read it, and pass it's results to use with the execute() 但是给您一个想法,您可以将文件作为参数发送到host_list(),然后读取它,并将其结果传递给execute()

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

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