使用 ARM 模板跨部署槽共享网站配置

Sharing web site config across deployment slots using an ARM template

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

理想情况下,我希望所有插槽都引用同一个 '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]",
          }
        }
      ]
    }
  ]
},

有什么方法可以使这个模板更易于维护吗?

或许您可以在模板中使用 copy

将包含您的插槽的部分移动到模板的根级别并添加:

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

名称属性应该如下所示:

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

这样你就可以说这个资源将是 WebSite 资源的子资源。有关此 here.

的更多信息

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

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

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

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

要使用这些给定值,您可以这样做:

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

这应该可以减少重复代码。

您好, 柯克