Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在每个任务的结构中隔离env.hosts的用户输入_Python_Python 2.7_Fabric - Fatal编程技术网

Python 如何在每个任务的结构中隔离env.hosts的用户输入

Python 如何在每个任务的结构中隔离env.hosts的用户输入,python,python-2.7,fabric,Python,Python 2.7,Fabric,我在fabric脚本中有一些任务。我试图弄清楚如何将env.hosts或@hosts装饰器的设置隔离到给定的任务。我希望在我的fab文件中有一些任务具有预设的主机,同时我可以将一个可以解析为主机元组的文件提供给其他文件。我还希望在运行时确定该文件 我有这个: def host_list(): host_file = raw_input("enter the file containing the list of hosts: ") host_list = open(host_fi

我在fabric脚本中有一些任务。我试图弄清楚如何将env.hosts或@hosts装饰器的设置隔离到给定的任务。我希望在我的fab文件中有一些任务具有预设的主机,同时我可以将一个可以解析为主机元组的文件提供给其他文件。我还希望在运行时确定该文件

我有这个:

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
我有一个任务:

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

当我将host_list函数分解为命令时,我可以正确设置env.hosts,但我还有其他任务,我不想让fabric提示符来设置env.hosts。我尝试在任务函数中添加步骤,但每次迭代都会得到提示。我试图向hosts装饰器提供host_list函数,但它给了我一个关于函数对象不可移植的错误。有没有办法将host_list函数仅隔离到某些任务?

这个答案在stack overflow中有很多问题。但为了给您一个想法,您可以将该文件作为参数发送到host_list,然后读取它,并将其结果传递给