Course contentsShow
← Cloud and Infrastructure
Lesson 952 of 2,98918. Google Cloud Deployment ManagerFree lesson

Jinja2 Templates for Parameterization

Use Jinja2 templating to create parameterized, reusable configuration templates.

Jinja2 Templates for Parameterization

Important Notice: Google Cloud Deployment Manager reached end of support on March 31, 2026. All new infrastructure work should be directed to Google Cloud Infrastructure Manager or Terraform. This content is provided strictly as a legacy reference for understanding and maintaining existing Deployment Manager configurations. Any gcloud deployment-manager commands or setups discussed are for maintaining existing deployments, not for new infrastructure.

You'll learn how Jinja2 templating operates within legacy Google Cloud Deployment Manager configurations to enable flexible, parameterized deployments.

What is Jinja2 Templating?

Jinja2 is a templating language that allows administrators to insert variables, logic, and loops into Deployment Manager templates. It acts much like a mail merge for infrastructure code—one core template is created with placeholders, which are populated with environment-specific values during deployment.

While Python templates provided full programming power in Deployment Manager, Jinja2 templates offered a simpler, more readable syntax for parameter substitution. In these legacy setups, YAML configurations include special Jinja2 markers that are replaced with actual values when parsing the deployment.

How Jinja2 Works in Deployment Manager

Instead of standard .yaml files, these templates use a .jinja extension. Inside, double curly braces {{ }} are used to output parameters, and {% %} are used for control structures like conditionals.

Basic parameter syntax:

resources:
- name: {{ properties["instanceName"] }}
  type: compute.v1.instance
  properties:
    machineType: zones/{{ properties["zone"] }}/machineTypes/{{ properties["machineType"] }}

In this structure, the values are injected from the primary YAML configuration file that imports the template via the properties key.

Conditional deployment:

{% if properties["environment"] == "production" %}
diskSizeGb: 100
{% else %}
diskSizeGb: 20
{% endif %}

This provided a clean approach for simple parameter substitution and conditional logic blocks when managing legacy infrastructure code.

Key Takeaway: In legacy Google Cloud Deployment Manager setups, Jinja2 templates provided a lightweight, readable way to parameterize configurations using placeholder syntax and simple logic, making infrastructure templates reusable across different environments.