简体   繁体   English

如何在app.yaml文件中使用环境变量?

[英]How do I use environment variables in app.yaml file?

例如,我偶然发现了此链接https://cloud.google.com/deployment-manager/configuration/create-configuration-file ,该链接在下面的示例中显示(特别是对于env)。

resources: - name: {{ env['name'] }} type: compute.v1.instance properties:

You can only set environment variables in app engine through the app.yaml and they can be accessed by your application instance. 只能通过app.yaml 在App Engine中 设置 环境变量,并且您的应用程序实例可以访问它们。

You can define environment variables in app.yaml to make them available to the app: 您可以在app.yaml中定义环境变量,以使其可用于应用程序:

 env_variables: MY_VAR: 'my value' 

access with os.environ 使用os.environ访问

import os
os.environ.get('MY_VAR', 'default value')

The app.yaml file must be valid YAML and must follow the app.yaml syntax . app.yaml文件必须是有效的YAML,并且必须遵循app.yaml语法

The example given isn't YAML. 给出的示例不是YAML。 It's a jinja template file used to create a configuration file. 这是一个用于创建配置文件的Jinja模板文件。

There might be simpler ways but here's a long shot: you can generate a valid app.yaml with the right environment variables (with their values) from a script. 可能有更简单的方法,但是app.yaml很长时间:您可以从脚本中使用正确的环境变量(及其值)生成有效的app.yaml

  • template.jinja - template to generate app.yaml template.jinja生成app.yaml的模板
  • generate_app_yaml.py - python script to generate app.yaml generate_app_yaml.py生成app.yaml的python脚本

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM