ARM模板条件嵌套资源,无模板链接

时间:2017-02-22 08:11:26

标签: azure azure-resource-manager arm-template

我正在尝试创建条件资源模板。开发环境不像生产环境那么强大,而且我在大多数情况下都成功地做到了这一点。但是,我似乎无法正确获得嵌套资源。

这是我的ARM模板的片段:

"webApp-resources": "[variables(concat('webApp-', parameters('env'), '-resources'))]",
"webApp-dev-resources": [],
"webApp-prod-resources": [
  {
    "name": "staging",
    "type": "Microsoft.Web/sites/slots",
    "location": "[resourceGroup().location]",
    "apiVersion": "2015-08-01",
    "dependsOn": [
      "[resourceId('Microsoft.Web/sites', variables('webApp-name'))]"
    ]
  }
],

这个想法很简单,资源变量是使用env参数组成的。 env参数可以是devprod,虽然这有效,但在尝试部署此模板时出现以下错误。

{
  "name": "[variables('webApp-name')]",
  "type": "Microsoft.Web/sites",
  ...
  "resources": "[variables('webApp-resources')]" // <- culprit!
},
  

请求内容无效且无法反序列化:&#39;错误转换值&#34; [变量(&#39; webApp-resources&#39;)]&#34;键入&#39; Microsoft.WindowsAzure.ResourceStack.Frontdoor.Templates.Schema.TemplateResource []&#39;。 Path&#39; properties.template.resources [1] .resources&#39;,第195行,第64位。&#39;

我还尝试将资源移动到变量中,并以类似的条件方式引用变量,非常类似于我们如何进行嵌套模板链接但没有模板链接。

resources: [
  "[variables('webApp-resource')]" // <- this doesn't work!
]

如果我正确回忆,这会导致类似的错误,但会出现不同的错误。

由此我得出结论,ARM模板语法并不是简单的查找和替换,我认为这是不好的,因为它确实使得更难以推断哪些有效,哪些无效。因为如果是的话,这将导致一个有效的模板。我通过将正确的值粘贴到资源部分来验证。

有没有人有类似的问题,你是如何解决这个问题的?

1 个答案:

答案 0 :(得分:3)

您应该能够在没有多个模板文件的情况下执行此操作,但不能在不使用嵌套部署的情况下执行此操作。因此,根据您要避免的内容,请尝试以下操作:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "env": {
      "type": "string",
      "allowedValues": [ "dev", "prod" ]
    }
  },
  "variables": {
    "resourceArray": "[variables(concat('resources', parameters('env')))]",
    "resourcesprod": [
        {
          "name": "as",
          "type": "Microsoft.Compute/availabilitySets",
          "location": "[resourceGroup().location]",
          "apiVersion": "2015-06-15",
          "dependsOn": [],
          "properties": {
          }
        }
      ],
    "resourcesdev": []
  },
    "resources": [
      {
        "name": "nest",
        "type": "Microsoft.Resources/deployments",
        "apiVersion": "2016-09-01",
        "dependsOn": [],
        "properties": {
          "mode": "Incremental",
          "template": {
            "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "resources": "[variables('resourceArray')]"
          },
          "parameters": {
          }
        }
      }
    ],
  "outputs": {}
}
相关问题