2017-09-15 87 views
1

我定義我的template.json內的alertrule與應在發生錯誤時提醒定製電子郵件。因此,JSON片段看起來是這樣的:Azure的ARM模板 - 使用數組變量

"resources": [ 
    { 
     "type": "microsoft.insights/alertrules", 
     "properties": { 
      "action": { 
       "customEmails": [ 
        "[email protected]", 
        "[email protected]" 
       ] 
      } 
     } 
    } 
] 

現在,我想這些電子郵件存儲爲一個數組變量,像這樣:

"variables": { 
    "alertEmails": [ 
     "[email protected]", 
     "[email protected]" 
    ] 
}, 
"resources": [ 
    { 
     "type": "microsoft.insights/alertrules", 
     "properties": { 
      "action": { 
       "customEmails": "[variables('alertEmails')]" 
      } 
     } 
    } 
] 

這並不工作,但我沒有沒有發現什麼是正確的語法。有人能告訴我嗎?

+0

請試試看遵守[蔚藍文件] (https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-enable-alerts-using-template)到use' 「customEmails」:「[分裂(參數( 'customEmailAddresses') ,「」)]「'‘customEmailAddresses’:‘逗號分隔的電子郵件地址,其中的警報也發送’ –

回答

1

這是不行的,但我沒有發現正確的語法是什麼。有人能告訴我嗎?

我用你提供的代碼測試它,它在我身邊正常工作。以下是我用過的模板。請嘗試使用以下演示代碼進行測試。

{ 
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 
    "contentVersion": "1.0.0.0", 
    "parameters": { 
    "serverFarmName": { 
     "type": "string", 
     "defaultValue": "serviceplan" 
    }, 
    "resourceId": { 
     "type": "string", 
     "defaultValue": "/subscriptions/{subscriptionId}/resourceGroups/{resourceName}/providers/Microsoft.Web/sites/websiteName", 
     "metadata": { 
     "description": "Resource ID of the resource emitting the metric that will be used for the comparison." 
     } 
    }, 
    "metricName": { 
     "type": "string", 
     "defaultValue": "BytesReceived", 
     "metadata": { 
     "description": "Name of the metric used in the comparison to activate the alert." 
     } 
    }, 
    "operator": { 
     "type": "string", 
     "defaultValue": "GreaterThan", 
     "allowedValues": [ 
     "GreaterThan", 
     "GreaterThanOrEqual", 
     "LessThan", 
     "LessThanOrEqual" 
     ], 
     "metadata": { 
     "description": "Operator comparing the current value with the threshold value." 
     } 
    }, 
    "threshold": { 
     "type": "string", 
     "defaultValue": "1", 
     "metadata": { 
     "description": "The threshold value at which the alert is activated." 
     } 
    }, 
    "aggregation": { 
     "type": "string", 
     "defaultValue": "Average", 
     "allowedValues": [ 
     "Average", 
     "Last", 
     "Maximum", 
     "Minimum", 
     "Total" 
     ], 
     "metadata": { 
     "description": "How the data that is collected should be combined over time." 
     } 
    }, 
    "windowSize": { 
     "type": "string", 
     "defaultValue": "PT5M", 
     "metadata": { 
     "description": "Period of time used to monitor alert activity based on the threshold. Must be between five minutes and one day. ISO 8601 duration format." 
     } 
    }, 
    "sendToServiceOwners": { 
     "type": "bool", 
     "defaultValue": true, 
     "metadata": { 
     "description": "Specifies whether alerts are sent to service owners" 
     } 
    }, 
    "webhookUrl": { 
     "type": "string", 
     "defaultValue": "", 
     "metadata": { 
     "description": "URL of a webhook that will receive an HTTP POST when the alert activates." 
     } 
    }, 
    "serverFarmResourceGroup": { 
     "type": "string", 
     "defaultValue": "resourceGroup" 
    } 
    }, 
    "variables": { 
    "alertEmails": [ 
     "[email protected]", 
     "[email protected]" 
    ], 


    "TomARMtestName": "[concat('TomARMtest', uniqueString(resourceGroup().id))]" 
    }, 
    "resources": [ 
    { 
     "type": "Microsoft.Insights/alertRules", 
     "name": "newalert", 
     "location": "[resourceGroup().location]", 
     "apiVersion": "2016-03-01", 
     "properties": { 
     "name": "newalert", 
     "description": "newalert", 
     "isEnabled": true, 
     "condition": { 
      "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition", 
      "dataSource": { 
      "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource", 
      "resourceUri": "[parameters('resourceId')]", 
      "metricName": "[parameters('metricName')]" 
      }, 
      "operator": "[parameters('operator')]", 
      "threshold": "[parameters('threshold')]", 
      "windowSize": "[parameters('windowSize')]", 
      "timeAggregation": "[parameters('aggregation')]" 
     }, 
     "actions": [ 
      { 
      "customEmails": "[variables('alertEmails')]" 
      } 
     ] 
     } 
    } 
    ] 
    , 
    "outputs": { 
    "out": { 
     "type": "array", 
     "value": "[variables('alertEmails')]" 
    } 
    } 
} 

我跟着azure document使用customEmails": "[split(parameters('customEmailAddresses'), ',')]"代碼,它也可以正常工作在我的身邊。

+0

哼,它確實有效。不知道我做錯了什麼... – Munchkin

+0

如果它有用,請將其標記爲有助於更多社區的答案。 –

1

如果你想使用數組,也許我們可以使用JSON這樣的:

"parameters": {  
"customEmailAddresses": { 
       "type": "array", 
       "defaultValue": ["[email protected]", 
        "[email protected]", 
        "[email protected]"] 

    } 
}, 

,並在行動上,是這樣的:

"customEmails": "[array(parameters('customEmailAddresses'))]"