简体   繁体   English

启动脚本以从Python导出环境变量

[英]Launching script to export environment variables from Python

I'm trying to remove all passwords and secrets from applications. 我正在尝试从应用程序中删除所有密码和机密。

To do this, I'm using environment variables. 为此,我正在使用环境变量。 I would like to avoid putting this specific stuff into ~/.bashrc or other built in files. 我想避免将这些特定内容放入~/.bashrc或其他内置文件中。 It would be ideal to call a specific script that exports the desired variables. 调用导出所需变量的特定脚本是理想的。

The reason for this is because the environment variables contain passwords and I'm trying to isolate them outside of all code except the environment scripts that set environment variables. 原因是环境变量包含密码,我试图将它们隔离在除设置环境变量的环境脚本之外的所有代码之外。

To summarize the desired flow: 总结所需的流程:

  1. Launch Python app 启动Python应用程序
  2. App spawns process that sets new environment variables 应用程序生成设置新环境变量的进程
  3. App pulls values from previously set environment variables App从先前设置的环境变量中提取值
  4. App uses those values in further processing App在进一步处理中使用这些值

contents of testpy.py testpy.py的内容

#!/usr/bin/env python

import os
import subprocess

env_file='/tmp/env_file'
db_pass_var='DBPASS'

#execute bash script that sets the env vars
subprocess.call([".", env_file], shell=True)

#try to get the variable set and print it
print os.getenv(db_pass, 'fail')

contents of /tmp/env_file / tmp / env_file的内容

#!/usr/bin/env bash

export DBUSER="myuser"
export DBPASS="mypass"

I've tried various methods like subprocess.Popen commands I found on other stackoverflow threads but nothing seems to work. 我尝试过各种方法,比如我在其他stackoverflow线程上找到的subprocess.Popen命令,但似乎没有任何工作。

Alternatives that I want to avoid: 我想避免的替代方案:

  • Running the env_file script first, then launching the python code. 首先运行env_file脚本,然后启动python代码。 The goal is to run it all from 1 application. 目标是从1个应用程序运行它。

  • Putting the passwords into a config file. 将密码放入配置文件中。 The goal is environment variables. 目标是环境变量。 Config files I would like to reserve to pointing to what variable to call, rather than setting the env vars themselves. 配置文件我想保留指向要调用的变量,而不是自己设置env变量。 This is because config files are checked into source control. 这是因为配置文件被检入源代码控制。 The only thing I want to leave out of source control is the actual export environment variables themselves which contain all sensitive environment info. 我唯一想要放弃源代码控制的是实际的export环境变量本身,它们包含所有敏感的环境信息。

There's an easier way. 有一种更简单的方法。 Just provide a wrapper script that sets the environment variables and then calls your main script. 只需提供一个包装器脚本来设置环境变量,然后调用主脚本。

It's convenient to use the envdir utility for this. 为此使用envdir实用程序很方便。 If you can't use that for some reason, then set env vars directly using os.environ and spawn off your main script as a child process. 如果由于某种原因无法使用它,那么直接使用os.environ设置env vars并将主脚本作为子进程生成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 脚本上应用源来导出环境变量? - Applying source on Python script to export environment variables? 从Python提示符运行Python脚本,以便将变量加载到交互式环境中 - Run a Python script from Python prompt such that variables are loaded into the interactive environment 将bash脚本中的环境变量分配给Python中的当前会话 - Assign environment variables from bash script to current session from Python 使用 Python 脚本设置环境环境变量 - Setting environment environment variables with a Python script 如何在 python 中使用同一 shell 中的脚本导出环境变量? - How do I export environment variables using a script in the same shell in python? 从SCONS运行的Python脚本无法访问环境变量 - Python script running from SCONS cannot access environment variables 采购文件以从python脚本中设置环境变量 - Sourcing a file to set environment variables from within a python script 从管道中的 python 脚本更新 Gitlab 环境变量 - Update Gitlab environment variables from a python script in a pipeline 从 python 设置的环境变量在 shell 脚本中不可见 - Environment variables set from python not visible in shell script Python 子进程导出环境变量(使用其他环境变量) - Python subprocess export environment variables (which use other environment variable)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM