Fabric2 中的环境变量

Rob*_*ham 5 python fabric

I\xe2\x80\x99m 使用 Python 3.6 和 Fabric 2.4。I\xe2\x80\x99m 使用 Fabric 通过 SSH 连接到服务器并运行一些命令。我需要为远程服务器上运行的命令设置环境变量。该文档表明类似的东西应该有效:

\n\n
from fabric import task\n\n@task(hosts=["servername"])\ndef do_things(c):\n    c.run("command_to_execute", env={"KEY": "VALUE"})\n
Run Code Online (Sandbox Code Playgroud)\n\n

但这不起作用。像这样的事情也应该是可能的:

\n\n
from fabric import task\n\n@task(hosts=["servername"])\ndef do_things(c):\n    c.config.run.env = {"KEY": "VALUE"}\n    c.run("command_to_execute")\n
Run Code Online (Sandbox Code Playgroud)\n\n

但这也不起作用。我觉得我\xe2\x80\x99m 缺少了一些东西。有人可以帮忙吗?

\n

小智 0

根据官方文档的那部分,对象connect_kwargs的属性Connection旨在替换env字典。我使用它,它按预期工作。