2

我在一个 ARM 模板中配置了几个 webapp,我发现我必须有很多重复的代码才能跨多个部署槽维护一个配置。依赖项和属性都必须单独复制和维护。我研究过使用变量,但是我的很多配置都依赖于其他资源,并且在评估变量时无法评估。

理想情况下,我希望所有插槽都引用相同的“Microsoft.Web/sites/config”对象,但我看不到这样做的方法。我当前的部署脚本看起来像这样(虽然这已经被大大简化了,但实际上我有更多的属性)

{
  "name": "[variables('siteName')]",
  "type": "Microsoft.Web/sites",
  "location": "[resourceGroup().location]",
  "apiVersion": "2015-08-01",
  "dependsOn": [
    "[concat('Microsoft.Web/serverfarms/', variables('serverfarm'))]",
    "[resourceid('Microsoft.EventHub/namespaces', variables('fullEventHubNameSpace'))]"
  ],
  "tags": {
    "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('siteName'))]": "Resource"
  },
  "properties": {
    "name": "[variables('siteName')]",
    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms/', variables('serverfarm'))]",
    "siteConfig": {
      "AlwaysOn": true
    }
  },
  "resources": [
    {
      "name": "appsettings",
      "type": "config",
      "apiVersion": "2015-08-01",
      "dependsOn": [
        "[concat('Microsoft.Web/sites/', variables('siteName'))]",
        "[concat('Microsoft.Insights/components/', variables('appInsightsSiteName'))]",
        "[concat('Microsoft.Web/sites/', variables('otherSiteName'))]",
        "[concat('Microsoft.DocumentDb/databaseAccounts/',variables('databaseAccountName'))]",
        "[resourceid('Microsoft.EventHub/namespaces', variables('fullEventHubNameSpace'))]"
      ],
      "properties": {
        "APPINSIGHTS_INSTRUMENTATIONKEY": "[reference(resourceId('Microsoft.Insights/components', variables('appInsightsName'))).InstrumentationKey]",
        "KEYVAULT_PATH": "[parameters('keyVaultPath')]",
        "KEYVAULT_SECRET": "[parameters('keyVaultSecret')]",
        "OTHER_SITE": "[reference(concat('Microsoft.Web/sites/', variables('otherSiteName'))).hostnames[0]]",
        "DB_KEY": "[listKeys(resourceId(concat('Microsoft.DocumentDb/databaseAccounts'),variables('databaseAccountName')),'2015-04-08').primaryMasterKey]",
      }
    },
    {
      "apiVersion": "2015-08-01",
      "name": "Staging",
      "type": "slots",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/Sites', variables('siteName'))]"
      ],
      "properties": {
      },
      "resources": [
        {
          "name": "appsettings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[concat('Microsoft.Web/sites/', variables('siteName'))]",
            "[concat('Microsoft.Insights/components/', variables('appInsightsName'))]",
            "[concat('Microsoft.DocumentDb/databaseAccounts/',variables('databaseAccountName'))]",
            "[concat('Microsoft.Web/sites/', variables('otherSiteName'))]",
            "[resourceid('Microsoft.EventHub/namespaces', variables('fullEventHubNameSpace'))]",
            "Staging"
          ],
          "properties": {
             "APPINSIGHTS_INSTRUMENTATIONKEY": "[reference(resourceId('Microsoft.Insights/components', variables('appInsightsName'))).InstrumentationKey]",
             "KEYVAULT_PATH": "[parameters('keyVaultPath')]",
             "KEYVAULT_SECRET": "[parameters('keyVaultSecret')]",
             "OTHER_SITE": "[reference(concat('Microsoft.Web/sites/', variables('otherSiteName'))).hostnames[0]]",
             "DB_KEY": "[listKeys(resourceId(concat('Microsoft.DocumentDb/databaseAccounts'),variables('databaseAccountName')),'2015-04-08').primaryMasterKey]",
          }
        }
      ]
    }
  ]
},

有没有办法让这个模板更易于维护?

4

1 回答 1

1

也许您可以在模板中使用副本

将带有插槽的部分移动到模板中的根级别并添加:

"copy": {
    "name": "slotcopy",
    "count": "[length(parameters('webSiteSlots'))]"
},

名称属性应如下所示:

"name": "[concat(parameters('webSiteName'), '/', parameters('webSiteSlots')[copyIndex()].name)]",

这样你就说这个资源将是 WebSite 资源的子资源。更多关于这里的信息。

现在您可以将带有复杂对象数组的参数添加到模板中:

"webSiteSlots": {
    "type": "array",
    "minLength": 0,
    "defaultValue": []
}

在您的参数文件中,您现在可以使用一些附加值设置您想要拥有的插槽集合:

"webSiteSlots": {
    "value": [
        {
            "name": "Staging",
            "environment": "Production"
        }
    ]
}

要使用这些给定的值,您可以执行以下操作:

"properties": {
    "ASPNETCORE_ENVIRONMENT": "[parameters('webSiteSlots')[copyIndex()].environment]"
}

这应该会减少重复的代码。

问候,柯克

于 2017-04-10T07:39:04.830 回答