複数のテンプレートの使用

このステップでは、他のテンプレートをインポートするテンプレートを探します。

これらのテンプレートを組み込んだ後、1 つのテンプレートを呼び出して、これらのすべてのテンプレートを含むデプロイを作成できるように構成します。

ネットワークのテンプレートを開く

compute-engine-template.py という名前のテンプレートを開きます。

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

nano compute-engine-template.py  # use your preferred text editor

このテンプレートにより、他のテンプレートがデプロイ内のすべてのリソースにインポートされます。vm-template.pyvm-template-2.py は仮想マシン(VM)用、network-template.py はネットワーク用、firewall-template.py はファイアウォール ルール用です。

# 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 Compute Engine."""

def GenerateConfig(context):
  """Creates the Compute Engine with network and firewall."""

  resources = [{
      'name': 'vm-1',
      'type': 'vm-template.py'
  }, {
      'name': 'vm-2',
      'type': 'vm-template-2.py'
  }, {
      'name': 'network-1',
      'type': 'network-template.py'
  }, {
      'name': 'firewall-1',
      'type': 'firewall-template.py'
  }]
  return {'resources': resources}

構成の表示

デプロイの構成ファイルを開きます。

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

nano config-with-many-templates.yaml  # use your preferred text editor

構成は他のテンプレートを直接呼び出していません。ただし、compute-engine-template.py は他のテンプレートに依存するため、他のテンプレートはインポートされていません。

# 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.

imports:
- path: vm-template.py
- path: vm-template-2.py
- path: network-template.py
- path: firewall-template.py
- path: compute-engine-template.py

resources:
- name: compute-engine-setup
  type: compute-engine-template.py

構成の保存とデプロイ

次のコマンドを実行して構成をデプロイします。

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

デプロイを表示するには、次のコマンドを実行します。

gcloud deployment-manager deployments describe deployment-with-many-templates

次のステップ: テンプレートのプロパティと環境変数

テンプレートのハードコード部分を、カスタム テンプレートや環境変数などの再利用可能なパターンと置き換えます。

デプロイの削除

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

gcloud deployment-manager deployments delete deployment-with-many-templates