Il supporto di Cloud Deployment Manager terminerà il 31 dicembre 2025. Se al momento utilizzi Deployment Manager, esegui la migrazione a Infrastructure Manager o a una tecnologia di deployment alternativa entro il 31 dicembre 2025 per assicurarti che i tuoi servizi continuino senza interruzioni.
Utilizzo di variabili di ambiente specifiche per il deployment
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Per ogni implementazione, Deployment Manager crea variabili di ambiente predefinite che contengono informazioni dedotte dall'implementazione.
Utilizza queste variabili di ambiente nei modelli Python o Jinja2 per ottenere informazioni sul progetto o sul deployment.
Le seguenti variabili di ambiente vengono impostate automaticamente da Deployment Manager.
Vengono sostituiti ovunque li utilizzi nei tuoi modelli. Ad esempio,
utilizza la variabile project_number per aggiungere il numero del progetto al nome di un
account di servizio.
Variabile di ambiente
Valore
deployment
Il nome del deployment.
name
Il name dichiarato nella configurazione che utilizza il
modello. Questo può essere utile se vuoi che il nome dichiarato nella configurazione sia il nome della risorsa nei modelli sottostanti.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-03 UTC."],[[["\u003cp\u003eDeployment Manager automatically creates environment variables for each deployment, providing information about the project and deployment.\u003c/p\u003e\n"],["\u003cp\u003eThese environment variables, such as \u003ccode\u003edeployment\u003c/code\u003e, \u003ccode\u003eproject\u003c/code\u003e, and \u003ccode\u003eproject_number\u003c/code\u003e, can be used within Jinja2 or Python templates to dynamically configure resources.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ecurrent_time\u003c/code\u003e environment variable provides a UTC timestamp of when the deployment expansion started.\u003c/p\u003e\n"],["\u003cp\u003eTemplates utilize environment variables using the \u003ccode\u003e{{ env["variable_name"] }}\u003c/code\u003e syntax for Jinja2 and \u003ccode\u003econtext.env["variable_name"]\u003c/code\u003e for Python.\u003c/p\u003e\n"],["\u003cp\u003eEnvironment variables can be used to define or alter resource names and properties within a template, such as appending the deployment name to a VM instance.\u003c/p\u003e\n"]]],[],null,["# Using deployment-specific environment variables\n\nFor each of your deployments, Deployment Manager creates pre-defined\nenvironment variables that contain information inferred from your deployment.\nUse these environment variables in your Python or Jinja2 templates to get\ninformation about your project or deployment.\n\nBefore you begin\n----------------\n\n- If you want to use the command-line examples in this guide, install the [\\`gcloud\\` command-line tool](/sdk).\n- If you want to use the API examples in this guide, set up [API access](/deployment-manager/docs/reference/latest).\n- Understand how to [create a basic template](/deployment-manager/docs/configuration/templates/create-basic-template).\n- Understand how to [create a configuration](/deployment-manager/docs/configuration/create-basic-configuration)\n\nAvailable environment variables\n-------------------------------\n\nThe following environment variables are automatically set by Deployment Manager.\nThey are replaced everywhere you use them in your templates. For example,\nuse the `project_number` variable to add the project number to the name of a\nservice account.\n\nUsing an environment variable\n-----------------------------\n\nUse the following syntax to add an environment variable to your templates: \n\n```django/jinja\n{{ env[\"deployment\"] }} # Jinja\n\ncontext.env[\"deployment\"] # Python\n```\n\nIn your template, use the variables as in these examples: \n\n### Jinja\n\n```django/jinja\n- type: compute.v1.instance\n name: vm-{{ env[\"deployment\"] }}\n properties:\n machineType: zones/us-central1-a/machineTypes/f1-micro\n serviceAccounts:\n - email: {{ env['project_number'] }}-compute@developer.gserviceaccount.com\n scopes:\n - ...\n```\n\n### Python\n\n```python\ndef GenerateConfig(context):\n resources = []\n resources.append ({\n 'name': 'vm-' + context.env[\"deployment\"],\n 'type': 'compute.v1.instance',\n 'properties': {\n 'serviceAccounts': [{\n 'email': context.env['project_number'] + '-compute@developer.gserviceaccount.com',\n 'scopes': [...]\n }]\n }\n ...}]\n return {'resources': resources}\n```\n\nWhat's next\n-----------\n\n- Add a template permanently to your project as a [composite type](/deployment-manager/docs/configuration/templates/create-composite-types).\n- [Host templates externally](/deployment-manager/docs/configuration/templates/hosting-templates-externally) to share with others.\n- Add [schemas](/deployment-manager/docs/configuration/templates/using-schemas) to ensure users interact with your templates correctly."]]