Fabric2中的环境变量 [英] Environment Variables in Fabric2

查看:217
本文介绍了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.

引用文档 :

是否将环境变量内联"作为前缀发送到命令字符串(export VARNAME=value && mycommand here)的前面,而不是尝试通过SSH协议本身提交它们(这是默认行为).如果远程服务器具有受限制的AcceptEnv设置(这是常见的默认设置),则很有必要.

Whether to send environment variables "inline" as prefixes in front of command strings (export VARNAME=value && mycommand here), instead of trying to submit them through the SSH protocol itself (which is the default behavior). This is necessary if the remote server has a restricted AcceptEnv setting (which is the common default).

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆