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
- If you want to use the command-line examples in this guide, install the `gcloud` command-line tool.
- If you want to use the API examples in this guide, set up API access.
- Understand how to create a basic configuration.
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:
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
Python
Your Python templates must meet these requirements:
The template must define a method called
GenerateConfig(context)orgenerate_config(context). If you use both method names in the same template, thegenerate_config()method takes precedence.The
contextobject 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