One advantage of using templates for your deployment is the ability to create and define custom properties, which let you reuse templates across zones, regions, and projects.
Template properties are arbitrary variables. Any configuration file or template file can provide a value for a template property without modifying the template. Therefore, you can change a property's value for various configurations without changing the template itself.
To reference an arbitrary value, use this syntax in a template:
context.properties["property-name"]
In addition to template properties, you can also use environment variables specific to your deployment, which are pre-populated with information about the deployment. You can use environment variables in templates to get unique information about your deployment.
You call an environment variable by using this syntax:
context.env['variable-name']
Valid environment variables include the deployment name, the project ID, the name property of your resource, and the type of your configuration. Learn more about environment variables.
Template properties and environment variables in a template
In this step, vm-template.py
shows the benefits of template properties and
environment variables. Open vm-template.py
:
cd deploymentmanager-samples/examples/v2/step_by_step_guide/step7_use_environment_variables/python
nano vm-template.py # use your preferred text editor
Various parts of the template have been replaced with template properties and
environment variables. For example, the project ID is replaced with
context.env[project]
, so you don't have to manually replace the project ID in
your templates.
The comments in the file describe other changes to the template.
Similarly, network-template.py
and firewall-template.py
use the deployment's
name in their definition, by calling context.env['name']
.
Deploying the configuration
To view the configuration file for this deployment, run the following command:
nano config-with-many-templates.yaml
Save your changes and redeploy your configuration to confirm the variables work.
gcloud deployment-manager deployments create deployment-with-template-properties --config config-with-many-templates.yaml
Deleting your deployment
We recommend that you delete the deployment to avoid charges. You don't need this deployment for the next step. Run the following command to delete the deployment:
gcloud deployment-manager deployments delete deployment-with-template-properties
Looking ahead: helper scripts
Next, learn about helper scripts to efficiently perform repeated tasks.