Template modules are helper files that perform specific functions that can make your templates more efficient. For example, you can have a module that generates unique names for your resources. Deployment Manager can execute any module written in Python or Jinja.
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 template.
- Understand how to create a configuration
Creating a template module
A template module is treated as a regular template file and can be written in Jinja or Python.
For example, the following is a helper template that generates a name given a prefix and suffix.
Jinja
In Jinja, this helper template (in this example, named helpers/common.jinja)
would look like the following:
You can then import this template and use it as a module. In your Jinja template, you could use the module like so:
The configuration must then import both files
(including the helpers/common.jinja
file):
The Deployment Manager service will expand the configuration and the final configuration looks like this:
resources:
- name: myfrontend-prod
type: compute.v1.instance
properties:
zone: us-central1-f
machineType: https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/machineTypes/f1-micro
disks:
- deviceName: boot
type: PERSISTENT
boot: true
autoDelete: true
initializeParams:
sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9
networkInterfaces:
- network: https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default
accessConfigs:
- name: External NAT
type: ONE_TO_ONE_NAT
Python
In Python, the helper template (in this example, named helpers/common.py)
would look like this:
To use it in your Python template:
The configuration must then import both files
(including the helpers/common.py
file):
Here is a more complicated helper module:
What's next
- Use template properties to abstract your content.
- Populate information about your projects and deployments using environment variables.
- Add a template permanently to your project as a composite type.