Creating a Basic Template

A basic configuration file might be enough for simple workloads but for more complex architectures or for configurations that you plan to reuse, you can break your configuration into templates.

A template is a separate file that is imported and used as a type in a configuration. You can use as many templates as you want in a configuration.

Templates allow you to separate your configuration out into different pieces that you can use and reuse across different deployments. Templates can be as generalized or specific as you need. With templates, you can also take advantage of features like template properties, environment variables, modules, and other template functionality to create dynamic configuration and template files.

For examples of templates that you can use in your own deployments, see the Deployment Manager GitHub repository.

Before you begin

Template syntax

Templates can be written in either Jinja 2.10.x or Python 3.x. Jinja maps more closely to the YAML syntax, so it might be easier to write templates in Jinja if you are more familiar with YAML.

You can also write template files in Python and take advantage of Python to programmatically generate parts of your templates. For example, you can use Python libraries to format template definitions. If you are familiar with Python, this might be a better format for you.

Deployment Manager accepts both Jinja and Python templates. You can import templates in both languages in the same configuration.

Creating a basic template

A template is a file you create, written in either Jinja or Python. For example, consider the following configuration file:

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

resources:
- name: vm-created-by-deployment-manager
  type: compute.v1.instance
  properties:
    zone: us-central1-a
    machineType: zones/us-central1-a/machineTypes/n1-standard-1
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-11
    networkInterfaces:
    - network: global/networks/default

This configuration is valid, but you can further simplify the configuration by breaking different parts as individual template files that you can reuse. To create a template based on the configuration above, pull out section for the resource in question and create a new Jinja or Python file.

The following snippets show the sections of templates that can simplify your deployment. For the full templates, click View on GitHub.

Jinja

- name: vm-template
  type: compute.v1.instance
  properties:
    zone: us-central1-a
    machineType: zones/us-central1-a/machineTypes/n1-standard-1
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-11
    networkInterfaces:
    - network: global/networks/default

Python

Your Python templates must meet these requirements:

  • The template must define a method called GenerateConfig(context) or generate_config(context). If you use both method names in the same template, the generate_config() method takes precedence.

    The context object contains metadata about the deployment and your environment, such as the deployment's name, the current project, and so on. Learn more about using deployment-specific environment variables.

  • The method must return a Python dictionary.

Other than that, it is up to you to generate the contents of your template.

Example

resources.append({
    'name': 'vm-template',
    'type':