Usar varias plantillas

En este paso, verás una plantilla que importa otras plantillas.

Después de incorporar estas plantillas, tu configuración solo tendrá que llamar a una plantilla para crear una implementación con todos estos recursos.

Abrir la plantilla de una cadena

Abre la plantilla llamada 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

Esta plantilla importa otras plantillas para todos los recursos de la implementación: vm-template.py y vm-template-2.py para las máquinas virtuales (VMs), network-template.py para una red y firewall-template.py para una regla de firewall.

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

Ver la configuración

Abre el archivo de configuración de la implementación:

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

Ten en cuenta que la configuración no ha llamado directamente a las otras plantillas. Sin embargo, las otras plantillas se importan porque compute-engine-template.py depende de ellas.

# 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

Guardar la configuración y desplegarla

Ejecuta este comando para implementar la configuración:

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

Para ver la implementación, ejecuta el siguiente comando:

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

Próximamente: propiedades de plantillas y variables de entorno

A continuación, sustituirá algunas partes predefinidas de las plantillas por patrones reutilizables, como plantillas personalizadas y variables de entorno.

Eliminar el despliegue

Te recomendamos que elimines la implementación para evitar que se te apliquen cargos. No necesitas este despliegue para el siguiente paso. Ejecuta el siguiente comando para eliminar la implementación:

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