2016-09-06 172 views
1

我在ARM模板中置备了几个webapps,并且我发现我必须拥有大量重复代码才能在多个部署插槽中维护单个配置。依赖性和属性必须分别复制和维护。我研究了一个变量,但是很多配置依赖于其他资源,并且在评估变量时无法评估。使用ARM模板跨部署插槽共享网站配置

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

有什么办法让这个模板更容易维护?

回答

1

也许你可以在你的模板中使用copy

与槽移动部分根级别在你的模板,并添加:

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

名称propperty shlould是这样的:

"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]" 
} 

这应该会减少公开的代码的公平性。

问候, 柯克

+0

会不会是把我的部署插槽到一个单独的web应用程序? –

+0

@IainBrown我错过了名字标签。这会给予关系。我在上面编辑了我的答案。 – KirKone

+0

好的,我会放弃它,谢谢 –