google-compute-engine - 使用环境变量的 Google Deployment Manager 模板

标签 google-compute-engine

我有一个名为 dev.yaml 的模板,如下所示:

imports:
- path: generate_config.py

resources:
- name: application_vm
  type: generate_config.py
  properties:
    zone: europe-west1-d
    project: cpb1234567

请注意,我正在对区域和项目进行硬编码。我希望它们来自这些环境变量。

CLOUDSDK_COMPUTE_ZONE=europe-west1-d
GCP_PROJ_ID=cpb1234567

generate_config.py 如下所示:

#!/usr/bin/env python

COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/'

def generate_config(context):

  resources = [{
      'name': context.env['name'],
      'type': 'compute.v1.instance',
      'properties': {
          'zone': context.properties['zone'],
          'machineType': ''.join([COMPUTE_URL_BASE, 
                                  'projects/', 
                                  context.properties['project'],
                                  '/zones/',
                                  context.properties['zone'],
                                  '/machineTypes/n1-standard-1']),
          'disks': [{
              'deviceName': 'boot',
              'type': 'PERSISTENT',
              'boot': True,
              'autoDelete': True,
              'initializeParams': {
                  'sourceImage': ''.join([COMPUTE_URL_BASE, 
                                          'projects/',
                                          context.properties['project'],
                                          '/global/images/jre-10gb-debian-jessie'])
              }
          }],
          'networkInterfaces': [{
              'network': ''.join([COMPUTE_URL_BASE, 
                                  'projects/',
                                  context.properties['project'],
                                  '/global/networks/default']),
              'accessConfigs': [{
                  'name': 'External NAT',
                  'type': 'ONE_TO_ONE_NAT'
              }]
          }]
      }
  }]
  return {'resources': resources}

我从属性中获取区域和项目,这样就可以了。

但是,如何从环境变量中获取区域和项目?

我似乎无法在 Python 模板中执行此操作:

import os
...
os.environ['GCP_PROJ_ID']

由于导入失败。

在 YAML 中,尚不清楚哪种语法有效。

非常感谢

内森

最佳答案

虽然不是这样的答案,但更多信息如下:

https://groups.google.com/forum/#!topic/gce-discussion/Md7rCo1ZMJY

关于google-compute-engine - 使用环境变量的 Google Deployment Manager 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44049937/

相关文章:

python - 云存储 : how to setup service account credentials for python boto library

ubuntu - GCE 实例崩溃后如何自动重启?

google-cloud-platform - 没有足够的资源来满足请求尝试不同的区域

google-compute-engine - Google 计算机引擎中/home/gke 中的未知用户

google-cloud-platform - gcloud : show quota limits for SimulateMaintenanceEvent

google-compute-engine - Google cloud_sql_proxy 无法连接到实例,流错误,protocol_error

google-app-engine - 谷歌应用引擎 : Budget and Daily Spending Limit not Working

google-cloud-platform - GPU 配额未显示在请求列表 (GCP) 中

google-compute-engine - 基于队列长度的谷歌计算引擎自动缩放

google-compute-engine - 无法创建套接字工厂 'com.google.cloud.sql.mysql.SocketFactory;'