ARM在复制迭代器中使用reference()时,arm模板解析器似乎中断了

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

我编写了一个ARM模板,可以根据JSON对象参数动态构建应用程序设置。这允许添加任何应用程序设置,而无需修改模板:

parameters.json

   {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {    
        "app_settings": {
          "value": {
            "keyvalue_pairs": [
              {
                "name": "appsetting1",
                "value": "value"
              },
              {
                "name": "ApplicationInsights:InstrumentationKey",
                "value": ""
              }
            ]
          }
        }
      }
    }

砍下工作template.json文件

 "resources": [
    {
      "comments": "",
      "type": "microsoft.insights/components",
      "kind": "web",
      "name": "[variables('app_service_name')]",
      "apiVersion": "2014-04-01",
      "location": "[resourceGroup().location]",
      "tags": {
        "[variables('hiddenlink_app_service')]": "Resource"
      },
      "scale": null,
      "properties": {
        "ApplicationId": "[variables('app_service_name')]"
      }
    },
    {
      "apiVersion": "2015-08-01",
      "name": "[concat(variables('app_service_name'),'/stage-slot')]",
      "type": "Microsoft.Web/sites/slots",
      "tags": {
        "displayName": "stage-slot"
      },
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/Sites', variables('app_service_name'))]"
      ],
      "properties": {
        "clientAffinityEnabled": false,
        "siteConfig": {
          "copy": [
            {
              "name": "appSettings",
              "count": "[length(parameters('app_settings').keyvalue_pairs)]",
              "input": {
                "name": "[parameters('app_settings').keyvalue_pairs[copyIndex('appSettings')].name]",
                "value": "[parameters('app_settings').keyvalue_pairs[copyIndex('appSettings')].value]"
              }
            }
          ]
        }
      }      
    }

我现在尝试在我的应用设置上有条件地引用app insights instrumentation键,希望从资源中覆盖app insights instrumentationkey。

"siteConfig": {
            "copy": [
              {
                "name": "appSettings",
                "count": "[length(parameters('app_settings').keyvalue_pairs)]",
                "input": {
                  "name": "[parameters('app_settings').keyvalue_pairs[copyIndex('appSettings')].name]",
                  "value": "[if(equals(parameters('app_settings').keyvalue_pairs[copyIndex('appSettings')].name,'ApplicationInsights:InstrumentationKey'),reference(variables('appInsightsResource')).InstrumentationKey,parameters('app_settings').keyvalue_pairs[copyIndex('appSettings')].value)]"
                }
              }
            ]
          }

这开始抛出错误,说if语句需要一个布尔第一个参数,但是我没有看到它有什么问题,所以我尝试了下面的代码片段它起作用,这让我相信在条件中使用“reference()”没有效:

"siteConfig": {
            "copy": [
              {
                "name": "appSettings",
                "count": "[length(parameters('app_settings').keyvalue_pairs)]",
                "input": {
                  "name": "[parameters('app_settings').keyvalue_pairs[copyIndex('appSettings')].name]",
                  "value": "[if(equals(parameters('app_settings').keyvalue_pairs[copyIndex('appSettings')].name,'ApplicationInsights:InstrumentationKey'),'testvalue',parameters('app_settings').keyvalue_pairs[copyIndex('appSettings')].value)]"
                }
              }
            ]
          }

另外,如果我删除整个“if()”并明确地将“reference(variables('appInsightsResource')).InstrumentationKey”放入值中,它会输出正确的值,因此我知道这个reference()调用有效,但在“if()”中添加时似乎会分解条件陈述。

问题是,有没有办法让这个工作?我正在尝试动态设置Instrumentation键,同时保持为我的应用程序设置传入JSON对象的能力

azure azure-resource-manager
1个回答
1
投票

我看到一般的复制属性构造非常令人困惑的结果。大多数时候都会出现绝对神秘的错误。

您将无法在reference()属性中使用count函数 - 在编译时扩展复制循环 - 在运行时评估引用。另外,今天我无法重现我的工作示例,但我有一个参考函数的工作示例在复制属性中工作,但没有if()。您可能想要创建一个最基本的示例,显示它不起作用(理想情况下只有1个资源)。如果这不起作用,您可能想在github和\或azure反馈上提出问题

您可以通过使用嵌套部署来解决此问题。但由于编译\运行时问题,通常以巧妙的方式使用属性复制有点痛苦。

© www.soinside.com 2019 - 2024. All rights reserved.