Niklas B
Niklas B

Reputation: 1969

Google Cloud Deployment Manager: Passing variables into templates

I'm using Google Cloud Deployment and I am trying to get external input into my template. Namely, I want to set a metadata variable on my instance (when creating the instance) but provide this value on execution.

I've tried:

gcloud deployment-manager deployments create  test-api-backend --config test-api-backend.yaml --properties 'my_value=hello'

Which fails (The properties flag should only be used when passing in a template as your config file.)

I've tried:

my_value=hello gcloud deployment-manager deployments create  test-api-backend --config test-api-backend.yaml

And use {{env['my_value']}} but the value isn't picked up.

I guess I could add the property in a .jinja file and re-write this file before I run everything, but it feels like a hack. That, or my idea of passing a variable from shell into Deploy Manager is a hack. I'm honestly not sure.

Upvotes: 0

Views: 1803

Answers (1)

Chris Crall
Chris Crall

Reputation: 26

As the error message indicates, the command line properties can only be used with a template. They are essentially meant to replace the config yaml file.

The easiest thing to do is to just rename your yaml file to a .py or .jinja file. Then use that template as the file in the gcloud command instead of the yaml file. In that new template file, add any defaults you would like if you don't pass them in on the command line. For python, something like:

if 'myparam' in context.properties:
    valuetouse = context.properties['myparam']
else:
    valuetouse = mydefaultvalue

If the template uses another template then you'll also need to create a schema file for the new, top level template so you can do the imports there instead of the yaml file. See the schema file in this github example.

https://github.com/GoogleCloudPlatform/deploymentmanager-samples/blob/master/examples/v2/igm-updater/ha-service.py.schema

If you want, you can ignore all the properties and just do the imports section.

Upvotes: 1

Related Questions