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

标签 azure azure-resource-manager

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

参数.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]"
              }
            }
          ]
        }
      }      
    }

我现在尝试在我的应用设置上有条件地引用应用见解检测 key ,希望覆盖资源中的应用见解检测 key 。

"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 语句需要一个 bool 值的第一个参数,但我没有发现它有任何问题,所以我尝试了以下代码片段,它起作用了,所以它让我相信使用“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 对象的能力

最佳答案

我发现复制属性构造的结果非常令人困惑。大多数时候都会因为绝对神秘的错误而崩溃。

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

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

关于azure - ARM 在复制迭代器中使用引用()时,arm 模板解析器似乎中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49103596/

相关文章:

azure - 在嵌套 ARM 模板中使用 resourceId 函数时出错

Azure Web 应用程序全局化

javascript - Node.js http 和 bing 搜索 api

powershell - Azure 帐户级 SAS token : Possible to Have a Policy?

azure - 如何使用 bicep 将父资源名称引用到模块内的资源

azure - 在 VS code 上添加参数文件以进行 Bicep 模板部署

azure - 如何检查CloudBlobDirectory是否存在?

azure - 具有 Microsoft.Owin.Security.OpenIdConnect 和 AzureAD v 2.0 端点的自定义参数

Azure ARM 模板 - 是否可以获取现有资源名称以将它们指定为新 ARM 部署中的参数或变量?

azure - 如何将现有的 Azure 资源组保存到资源管理器模板 JSON 文件中?