Python 结构未拾取环境主机更改

Python 结构未拾取环境主机更改,python,automation,fabric,Python,Automation,Fabric,我正在尝试根据每个任务更换主机。这是我的密码: @task def process(): execute(get_file) execute(transfer_file) @task def get_file(): env.hosts = [hexi_host] 这实际上是唯一相关的代码。基本上,我不会在get_file任务之外的任何地方定义env.hosts。当我运行脚本时,我得到: 找不到主机。请为连接指定(单个)主机字符串: 即使我在get_文件中定义了它。若我在处理之前定

我正在尝试根据每个任务更换主机。这是我的密码:

@task
def process():
  execute(get_file)
  execute(transfer_file)

@task
def get_file():
  env.hosts = [hexi_host]
这实际上是唯一相关的代码。基本上,我不会在get_file任务之外的任何地方定义env.hosts。当我运行脚本时,我得到:

找不到主机。请为连接指定(单个)主机字符串:


即使我在get_文件中定义了它。若我在处理之前定义了它,那个么它会工作,但以后我再也不能切换到其他主机。希望这有意义,非常感谢您的帮助。

在调用execute()函数之前,您必须先设置主机

@task
def process():
    env.roledefs = {'receiver': [hexi_host], 'sender': [otherhosts]}
    execute(get_files)
    execute(transfer_files)

@task
@roles('receiver')
def get_files():
    run(...)
另请参见:

请参见类似内容。