Шаблон Azure ARM - использование переменной массива

Я определяю правило оповещения внутри своего template.json с настраиваемыми электронными сообщениями, которые будут предупреждены в случае ошибки. Итак, фрагмент JSON выглядит так:

"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')]"
            }
        }
    }
]

Это не работает, но я не выяснил, каков правильный синтаксис. Кто-нибудь может мне сказать?


person Munchkin    schedule 15.09.2017    source источник
comment
Попробуйте следовать лазурный документ для использования"customEmails": "[split(parameters('customEmailAddresses'), ',')]", customEmailAddresses: разделенные запятыми адреса электронной почты, на которые также отправляются оповещения.   -  person Tom Sun - MSFT    schedule 15.09.2017


Ответы (2)


Это не работает, но я не выяснил, каков правильный синтаксис. Кто-нибудь может мне сказать?

Я тестирую это с помощью предоставленного вами кода, у меня он работает правильно. Ниже приводится мой использованный шаблон. Пожалуйста, попробуйте протестировать с помощью следующего демонстрационного кода.

{
  "$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')]"
    }
  }
}

И я следую лазурному документу для использования customEmails": "[split(parameters('customEmailAddresses'), ',')]" кода, он также корректно работает на моей стороне.

person Tom Sun - MSFT    schedule 15.09.2017
comment
Если это полезно, отметьте его как ответ, который поможет большему количеству сообществ. - person Tom Sun - MSFT; 18.09.2017

Если вы хотите использовать массив, возможно, мы можем использовать json следующим образом:

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

  }
},

и в действии вот так:

 "customEmails": "[array(parameters('customEmailAddresses'))]"
person Jason Ye    schedule 15.09.2017