前记
利用 Puppet
做远程脚本执行的时候,发现 Python
脚本无法获取到环境变量
解决
在执行脚本前去获取到环境变量,这样就可以获取到环境变量了,或者重写 os.environ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| def get_env(): try: source_profile_cmd = "source /etc/profile && env" status, out = commands.getstatusoutput(source_profile_cmd) envs = {} for info in out.split("\n"): env = info.split("=") if len(env) == 2: envs[env[0]] = env[1] return envs except: return {} get_env()
|