Azure ARM-基于变量的资源名称

问题描述 投票:0回答:1

我的目标是为要部署到的所有环境提供一个ARM模板。

[我希望能够将变量定义为发布管道的一部分,例如'dev'或'prod',然后让ARM模板将其用作已部署资源名称的一部分。

例如:

myapi-dev-appserviceplanmyapi-prod-appserviceplan

azure deployment azure-devops continuous-deployment continuous-delivery
1个回答
0
投票

只需创建参数并在JSON文件中使用它们。请注意,这里有“参数”和“变量”。就我而言,我使用参数来创建变量,但有时我直接使用参数作为环境名称(即,在我的情况下,环境为“ dev”,“ uat”,“ prd”并注入资源名称)

这里是一个完整的示例,显示您想做什么

{
    "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": { 
        "environment": {
            "type": "String" 
        },
        "sku": {
            "type": "string",
            "defaultValue": "S1"
        }
    },
    "variables": {
        "webappPrefix": "lovelyfront",
      "location": "westeurope",
        "aiName": "[concat('ai-',variables('webappPrefix'), '-', parameters('environment'))]",
        "webappName": "[concat('wa-',variables('webappPrefix'), '-', parameters('environment'))]",
        "webappNameStagingSlot": "[concat(variables('webappName'), '/', 'staging')]",
        "appServicePlanName": "[concat('asp-',variables('webappPrefix'), '-', parameters('environment'))]",
        "storageAccountName": "[concat('stolovefront', toLower(parameters('environment')))]",
        "cognitiveEndpointName": "[concat('cog-',variables('webappPrefix'), '-', parameters('environment'))]",
        "signalRName": "[concat('sig-',variables('webappPrefix'), '-', parameters('environment'))]"
    },
  "resources": [
    {
      "apiVersion": "2014-04-01",
      "name": "[variables('aiName')]",
      "type": "Microsoft.Insights/components",
      "location": "[variables('location')]",
      "properties": {
        "ApplicationId": "[variables('aiName')]"
      }
    },
    {
      "apiVersion": "2017-08-01",
      "type": "Microsoft.Web/serverfarms",
      "kind": "app",
      "name": "[variables('appServicePlanName')]",
      "location": "[variables('location')]",
      "properties": {},
      "dependsOn": [],
      "sku": {
        "name": "[parameters('sku')]"
      }
    },
© www.soinside.com 2019 - 2024. All rights reserved.