Azure ARM 模板为数组参数中的每个值添加前缀
Azure ARM template prefix each value in array parameter
在 scheduledQueryRules
元素中,我们从参数文件中传递 aznsAction.actionGroup
(数组)。
参数如下所示:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"actionGroups": {
"value": [
"ActionGroupName1",
"ActionGroupName2",
"ActionGroupName3",
"ActionGroupName4"
]
}
}
}
然后我们像这样将它传递给 ARM 模板:
...
"action": {
"odata.type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.Microsoft.AppInsights.Nexus.DataContracts.Resources.ScheduledQueryRules.AlertingAction",
"severity": "[variables('alertActions').SeverityLevel]",
"aznsAction": {
"actionGroup": "[array(parameters('actionGroups'))]"
},
"trigger": {
"thresholdOperator": "[variables('alertTrigger').Operator]",
"threshold": "[variables('alertTrigger').Threshold]"
}
}
...
应使用resourceId(..)
函数选择ActionGroup,但参数文件不支持此功能。
当我们用完整的资源路径替换 actionGroups
值时,部署工作正常,如下所示:
"actionGroups": {
"value": [
"/subscriptions/{id}/resourceGroups/{rg}/providers/microsoft.insights/actiongroups/ActionGroupName1",
"/subscriptions/{id}/resourceGroups/{rg}/providers/microsoft.insights/actiongroups/ActionGroupName2",
"/subscriptions/{id}/resourceGroups/{rg}/providers/microsoft.insights/actiongroups/ActionGroupName3",
"/subscriptions/{id}/resourceGroups/{rg}/providers/microsoft.insights/actiongroups/ActionGroupName4"
]
}
但是由于我不想对订阅和资源组进行硬编码,所以我正在寻找一种解决方案,我们可以在将其传递给 aznsAction.actionGroup
之前为各个数组值添加前缀。
我认为这里不能使用copy
。
基本上我想要这样的东西:
"aznsAction": {
"actionGroup": "[select[array(parameters('actionGroups'))], concat(resourceId(..), parameters('actionGroups')[iterator()])]"
}
有没有可能达到这样的效果?
因此,如果您有一个包含这样的操作组名称的参数:
"parameters": {
"actionGroups": {
"type": "array",
"value": [
"ActionGroupName1",
"ActionGroupName2",
"ActionGroupName3",
"ActionGroupName4"
]
}
}
您可以创建一个变量来将数组转换为操作组 ID 数组:
"variables": {
"copy": [
{
"name": "actionGroupIds",
"count": "[length(parameters('actionGroups'))]",
"input": "[resourceId('Microsoft.Insights/ActionGroups', parameters('actionGroups')[copyIndex('actionGroupIds')])]"
}
]
}
那你就可以这样使用了:
"aznsAction": {
"actionGroup": "[variables('actionGroupIds')]"
}
在 scheduledQueryRules
元素中,我们从参数文件中传递 aznsAction.actionGroup
(数组)。
参数如下所示:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"actionGroups": {
"value": [
"ActionGroupName1",
"ActionGroupName2",
"ActionGroupName3",
"ActionGroupName4"
]
}
}
}
然后我们像这样将它传递给 ARM 模板:
...
"action": {
"odata.type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.Microsoft.AppInsights.Nexus.DataContracts.Resources.ScheduledQueryRules.AlertingAction",
"severity": "[variables('alertActions').SeverityLevel]",
"aznsAction": {
"actionGroup": "[array(parameters('actionGroups'))]"
},
"trigger": {
"thresholdOperator": "[variables('alertTrigger').Operator]",
"threshold": "[variables('alertTrigger').Threshold]"
}
}
...
应使用resourceId(..)
函数选择ActionGroup,但参数文件不支持此功能。
当我们用完整的资源路径替换 actionGroups
值时,部署工作正常,如下所示:
"actionGroups": {
"value": [
"/subscriptions/{id}/resourceGroups/{rg}/providers/microsoft.insights/actiongroups/ActionGroupName1",
"/subscriptions/{id}/resourceGroups/{rg}/providers/microsoft.insights/actiongroups/ActionGroupName2",
"/subscriptions/{id}/resourceGroups/{rg}/providers/microsoft.insights/actiongroups/ActionGroupName3",
"/subscriptions/{id}/resourceGroups/{rg}/providers/microsoft.insights/actiongroups/ActionGroupName4"
]
}
但是由于我不想对订阅和资源组进行硬编码,所以我正在寻找一种解决方案,我们可以在将其传递给 aznsAction.actionGroup
之前为各个数组值添加前缀。
我认为这里不能使用copy
。
基本上我想要这样的东西:
"aznsAction": {
"actionGroup": "[select[array(parameters('actionGroups'))], concat(resourceId(..), parameters('actionGroups')[iterator()])]"
}
有没有可能达到这样的效果?
因此,如果您有一个包含这样的操作组名称的参数:
"parameters": {
"actionGroups": {
"type": "array",
"value": [
"ActionGroupName1",
"ActionGroupName2",
"ActionGroupName3",
"ActionGroupName4"
]
}
}
您可以创建一个变量来将数组转换为操作组 ID 数组:
"variables": {
"copy": [
{
"name": "actionGroupIds",
"count": "[length(parameters('actionGroups'))]",
"input": "[resourceId('Microsoft.Insights/ActionGroups', parameters('actionGroups')[copyIndex('actionGroupIds')])]"
}
]
}
那你就可以这样使用了:
"aznsAction": {
"actionGroup": "[variables('actionGroupIds')]"
}