使用多個範本

在這個步驟中,您將探索匯入其他範本的範本。

整合這些範本後,設定只需要呼叫單一範本,即可建立具有所有這些資源的部署作業。

開啟網路適用的範本

開啟名為 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) 適用的 vm-template.pyvm-template-2.py、網路適用的 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