【问题标题】:Is it possible to do an iterative string replace in an Azure ARM template?是否可以在 Azure ARM 模板中进行迭代字符串替换?
【发布时间】:2019-04-04 11:41:20
【问题描述】:

我怀疑不可能做我正在寻找的事情,但值得一试!

我有一个用于预配 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】:

    取决于最终结果应该是什么样子,您可以通过不同的方式执行此操作,但我建议您不要在模板中执行此操作,并建议您在模板之外执行此操作并输入结果。如果您真的想完全实现您所描述的,您可以使用嵌套部署来做到这一点**。我认为没有其他方法可以遍历数组以在 ARM 模板中构建字符串。

    【讨论】:

    • 谢谢。我确实怀疑这是不可能的。我的理想是驱动参数文件中的所有内容以保持模板和构建步骤通用,但感谢您的想法。我一直在尝试递归 ARM 函数,但这是不允许的!
    • 是的,您基本上需要将嵌套部署用作函数,并逐个提供输入并收集输出并将先前的部署输出也作为输入传递。如果您对此有疑问 - 您可以回到这里并提出另一个问题。我可以写出来
    【解决方案2】:

    这是我能来的最接近的。我们在 powershell 中为 secretObjects 进行输入循环。

     {
          "type": "Microsoft.KeyVault/vaults/secrets",
    

    "name": "[concat(variables('KeyVaultName'), '/', 变量('secretsObject').secrets[copyIndex()].secretName)]",

    "apiVersion": "2018-02-14",
          "dependsOn": [
            "[concat('Microsoft.KeyVault/vaults/', variables('KeyVaultName'))]"
          ],
          "copy": {
            "name": "secretsCopy",
            "count": "[length(variables('secretsObject').secrets)]"
          },
          "properties": {
            "value": "[variables('secretsObject').secrets[copyIndex()].secretValue]"
          }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      相关资源
      最近更新 更多