是否可以在Azure ARM模板中执行迭代字符串替换?

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

我怀疑不可能做我正在寻找的东西,但它值得一试!

我有一个用于配置Azure日志查询警报规则的管道。各个警报规则被定义为ARM参数文件,我使用共享的ARM模板文件来部署它们。

这是我的模板文件的精简版本,省略了大部分参数。

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "logQuery": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "Query to execute against the AI resource"
            }
        }
    },
    "variables": {
        "appInsightsResourceId": "[concat(resourceGroup().id,'/providers/','microsoft.insights/components/', parameters('appInsightsResourceName'))]",
        "actionGroupId": "[concat(resourceGroup().id,'/providers/','microsoft.insights/actionGroups/', parameters('actionGroupName'))]",
        "linkToAiResource" : "[concat('hidden-link:', variables('appInsightsResourceId'))]"
    },
    "resources":[{
        "name":"[parameters('alertName')]",
        "type":"Microsoft.Insights/scheduledQueryRules",
        "location": "northeurope",
        "apiVersion": "2018-04-16",
        "tags": {
            "[variables('linkToAiResource')]": "Resource"
        },
        "properties":{
           "description": "[parameters('alertDescription')]",
           "enabled": "[parameters('isEnabled')]",
           "source": {
               "query": "[parameters('logQuery')]",
               "dataSourceId": "[variables('appInsightsResourceId')]",
               "queryType":"[parameters('logQueryType')]"
           },
          "schedule":{
               "frequencyInMinutes": "[parameters('alertSchedule').Frequency]",
               "timeWindowInMinutes": "[parameters('alertSchedule').Time]"    
           },
           "action":{
                "odata.type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.Microsoft.AppInsights.Nexus.DataContracts.Resources.ScheduledQueryRules.AlertingAction",
                "severity": "[parameters('alertSeverity')]",
                "aznsAction":{
                    "actionGroup":"[array(variables('actionGroupId'))]"
                },
                "trigger":{
                    "thresholdOperator":"[parameters('alertTrigger').Operator]",
                    "threshold":"[parameters('alertTrigger').Threshold]"
                }
            }
         }
       }
    ]
}

您可以看到我如何将App Insights查询作为参数提供,因此我的参数文件可能如下所示:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0", 
    "parameters": {
        "logQuery": {
            "value": "requests | where resultCode >= 500"
        }
    }
}

但是,当查看为不可破解的JSON字符串时,这些查询可能会很长并且难以理解。所以我想参数化这个参数(如果你知道我的意思),这样就可以单独定义和提供关键变量。我正在考虑将参数更改为类似的内容,引入一个新参数,为参数化查询保存一个占位符替换数组...

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0", 
    "parameters": {
        "logQueryVariables": [
            { "{minCode}": "500" }
        ],
        "logQuery": {
            "value": "requests | where resultCode >= {minCode}"
        }
    }
}

...然后找到一种方法迭代变量数组并替换logQuery参数中的占位符,我想也许我可以使用ARM函数或其他东西。但是我不敢承认我被这部分困住了。是否可以使用copy语法来做这样的事情?

azure azure-devops
1个回答
0
投票

取决于最终结果应该是什么样,你可以采用不同的方式,但我建议不要在模板中执行此操作,并建议您在模板外执行此操作并输入结果。如果您真的想要实现您所描述的内容,那么您可以使用嵌套部署来实现这一目标。我不认为有任何其他方式迭代数组在ARM模板中构建一个字符串。

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