本文介绍了Fabric2中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.6和Fabric 2.4.我正在使用Fabric将SSH连接到服务器并运行一些命令.我需要为在远程服务器上运行的命令设置一个环境变量.该文档表明这样的事情应该起作用:

I’m using Python 3.6 and Fabric 2.4. I’m using Fabric to SSH into a server and run some commands. I need to set an environment variable for the commands being run on the remote server. The documentation indicates that something like this should work:

from fabric import task

@task(hosts=["servername"])
def do_things(c):
    c.run("command_to_execute", env={"KEY": "VALUE"})

但这是行不通的.这样的事情也应该是可能的:

But that doesn’t work. Something like this should also be possible:

from fabric import task

@task(hosts=["servername"])
def do_things(c):
    c.config.run.env = {"KEY": "VALUE"}
    c.run("command_to_execute")

但这也不起作用.我觉得我想念一些东西.有人可以帮忙吗?

But that doesn’t work either. I feel like I’m missing something. Can anyone help?

推荐答案

在创建Connection对象时,请尝试添加inline_ssh_env=True.

When creating the Connection object, try adding inline_ssh_env=True.

引用文档 :

这篇关于Fabric2中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:20