テンプレート プロパティの設定と環境変数の使用

デプロイにテンプレートを使用すると、カスタム プロパティの作成と定義を行うことができます。これにより、ゾーン、リージョン、プロジェクト間でテンプレートを再利用できます。

テンプレート プロパティは任意の変数です。構成ファイルやテンプレート ファイルでは、テンプレートを変更せずにテンプレート プロパティの値を指定できます。テンプレート自体を変更せずに、さまざまな構成に合わせてプロパティの値を変更できます。

任意の値を参照するには、テンプレートで次の構文を使用します。

context.properties["property-name"]

テンプレート プロパティの他に、デプロイ固有の環境変数を使用することもできます。これらの環境変数には、デプロイに関する情報が事前に格納されます。テンプレートで環境変数を使用すると、デプロイに関する固有の情報を取得できます。

環境変数を呼び出すには、次の構文を使用します。

context.env['variable-name']

有効な環境変数には、デプロイ名、プロジェクト ID、使用するリソースの name プロパティ、使用する構成のタイプなどがあります。詳しくは、環境変数をご覧ください。

テンプレート プロパティとテンプレートの環境変数

このステップでは、vm-template.py を使用してテンプレート プロパティと環境変数のメリットについて説明します。vm-template.py を開きます。

cd deploymentmanager-samples/examples/v2/step_by_step_guide/step7_use_environment_variables/python

nano vm-template.py  # use your preferred text editor

テンプレートのさまざまな部分が、テンプレート プロパティや環境変数に置き換えられています。たとえば、プロジェクト ID は context.env[project] に置き換えられています。テンプレート内のプロジェクト ID を手動で置換する必要はありません。

ファイル内のコメントには、テンプレートに対するその他の変更が記述されています。

# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Creates the virtual machine."""

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

def GenerateConfig(context):
  """Creates the virtual machine with environment variables."""

  resources = [{
      # `the-first-vm` is replaced with `context.env[`name`]`
      'name': context.env['name'],
      'type': 'compute.v1.instance',
      'properties': {
          # All occurrences of `us-central1-f` are replaced with
          # `context.properties[`zone`].
          # All occurrences of the project ID are replaced with
          # `context.env[`project`]`.
          # `f1-micro` is replaced with `context.properties["machineType"].
          'zone': context.properties['zone'],
          'machineType': ''.join([COMPUTE_URL_BASE, 'projects/',
                                  context.env['project'], '/zones/',
                                  context.properties['zone'], '/machineTypes/',
                                  context.properties['machineType']]),
          'disks': [{
              'deviceName': 'boot',
              'type': 'PERSISTENT',
              'boot': True,
              'autoDelete': True,
              'initializeParams': {
                  'sourceImage': ''.join([COMPUTE_URL_BASE, 'projects/',
                                          'debian-cloud/global/',
                                          'images/family/debian-11'])
              }
          }],
          # `$(ref.a-new-network.selfLink)` is replaced with
          # `$(ref.` + context.properties[`network`] + `selfLink)`.
          'networkInterfaces': [{
              'network': '$(ref.' + context.properties['network']
                         + '.selfLink)',
              'accessConfigs': [{
                  'name': 'External NAT',
                  'type': 'ONE_TO_ONE_NAT'
              }]
          }]
      }
  }]
  return {'resources': resources}

同様に、network-template.pyfirewall-template.py は、context.env['name'] を呼び出し、定義内のデプロイ名を使用しています。

構成のデプロイ

このデプロイの構成ファイルを表示するには、次のコマンドを実行します。

nano config-with-many-templates.yaml

変更を保存して構成を再度デプロイし、変数が機能していることを確認します。

gcloud deployment-manager deployments create deployment-with-template-properties --config config-with-many-templates.yaml

デプロイの削除

費用の発生を防ぐため、デプロイを削除することをおすすめします。このデプロイは次のステップで必要ありません。次のコマンドを実行して、デプロイを削除します。

gcloud deployment-manager deployments delete deployment-with-template-properties

次のステップ: ヘルパー スクリプト

繰り返し行うタスクを効率的に実行するためにヘルパー スクリプトを利用します。