Helper scripts, or template modules, are helper files that can make your templates more efficient by performing specific functions. For example, you can use helper scripts to interpret resource metadata, create files, and launch services.
You will now explore a Python helper script that names a virtual machine, given a prefix and a suffix.
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.
Opening the helper script
The basic helper script in this example generates the name for a virtual machine (VM). To view the script, run these commands:
cd deploymentmanager-samples/examples/v2/step_by_step_guide/create_a_helper_script
nano common.py # use your preferred text editor
The GenerateMachineName()
function takes a prefix and suffix, and generates a
name in the format prefix-suffix
:
Using the helper script in the template
To use common.py
in vm-template.py
, several changes must be made to the
template.
To view the changes, open vm-template.py
:
nano vm-template.py
The template contains code comments that highlight the changes.
Note that the template imports common.py
at the top of the file.
In the resources
section, the name
fields for the VMs now call
GenerateMachineName()
.
Viewing the changes to the configuration
To view the updated configuration, open two-vms.yaml
:
nano two-vms.yaml
Note that the helper script common.py
must be imported in the configuration
as well.
Deploy your configuration:
gcloud deployment-manager deployments create deployment-with-helper-script --config two-vms.yaml
To view the deployment, including the resources with the generated names, run:
gcloud deployment-manager deployments describe deployment-with-helper-script
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-helper-script
Looking ahead: updating deployments
Next, learn to add, delete, and change the properties of resources in a deployment as your app evolves.