Azure RM模板。有条件地部署复制资源

时间:2019-06-19 08:37:29

标签: azure azure-resource-manager arm-template azure-template azure-rm

如果数组参数中有4个以上的条目,则需要部署特定的资源。我能够使用5个(或更多)条目来执行此操作,但是我还需要部署不因3个或更少的条目而失败,而根本不要创建该资源。现在,我收到3个或更少条目的以下错误:

  

错误:代码= InvalidTemplate; Message =部署模板验证   失败:“第56行”和“ 19”列的“模板”副本定义   的副本数无效。复制计数必须为正整数   值,并且不能超过“ 800”。请参阅https://aka.ms/arm-copy   使用情况的详细信息。'。

我尝试为资源添加条件:

...
  "resources": [
    {
      "condition": "[greater(length(parameters('apps')),4)]",
      "name": "[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]",
...

甚至:

...
  "resources": [
    {
      "condition": false,
      "name": "[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]",
...

,但仍然出现相同的错误。 这是模板:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "apps": {
      "type": "array",
      "defaultValue": [
        {
          "name": "name1",
          "value": "111"
        },
        {
          "name": "name2",
          "value": "222"
        },
        {
          "name": "name3",
          "value": "333"
        },
        {
          "name": "name4",
          "value": "444"
        },
        {
          "name": "webtest5",
          "value": "555"
        }
      ]
    },
    "existingApplicationInsightsName": {
      "type": "string",
      "defaultValue": "appname1"
    }
  },
  "variables": {},
  "resources": [
    {
      "name": "[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]",
      "apiVersion": "2015-05-01",
      "type": "microsoft.insights/webtests",
      "location": "westeurope",
      "tags": {
        "[concat('hidden-link:', resourceId('microsoft.insights/components/', parameters('existingApplicationInsightsName')))]": "Resource"
      },
      "properties": {
        "SyntheticMonitorId": "[parameters('apps')[copyIndex(4)].name]",
        "Name": "[parameters('apps')[copyIndex(4)].name]",
        "Enabled": true,
        "Frequency": 300,
        "Timeout": 120,
        "Kind": "ping",
        "RetryEnabled": true,
        "Locations": [
          {
            "Id": "us-ca-sjc-azr"
          }
        ],
        "Configuration": {
          "WebTest": "[concat('<WebTest Name=\"', parameters('apps')[copyIndex(4)].name, '\"',  ' Id=\"', '9d420f1a-f797-427a-804c-f37373eefc82' ,'\"    Enabled=\"True\" CssProjectStructure=\"\" CssIteration=\"\" Timeout=\"0\" WorkItemIds=\"\" xmlns=\"http://microsoft.com/schemas/VisualStudio/TeamTest/2010\" Description=\"\" CredentialUserName=\"\" CredentialPassword=\"\" PreAuthenticate=\"True\" Proxy=\"default\" StopOnError=\"False\" RecordedResultFile=\"\" ResultsLocale=\"\">        <Items>        <Request Method=\"GET\" Guid=\"a5f10126-e4cd-570d-961c-cea43999a200\" Version=\"1.1\" Url=\"', 'http://www.microsoft.com' ,'\" ThinkTime=\"0\" Timeout=\"300\" ParseDependentRequests=\"True\" FollowRedirects=\"True\" RecordResult=\"True\" Cache=\"False\" ResponseTimeGoal=\"0\" Encoding=\"utf-8\" ExpectedHttpStatusCode=\"', 200 ,'\" ExpectedResponseUrl=\"\" ReportingName=\"\" IgnoreHttpStatusCode=\"False\" /></Items></WebTest>')]"
        }
      },
      "copy": {
        "name": "createWebTests",
        "count": "[sub(length(parameters('apps')),4)]"
      }
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

尝试这样做:

"condition": "[greater(length(parameters('apps')),4)]",

并设置您要复制到的位置:

"copy": {
  "name": "createWebTests",
  "count": "[if(greater(length(parameters('apps')),4), sub(length(parameters('apps')),4), 1)]"
}

这应该可以解决以下情况:在您的情况下,计数为负,并且当阵列中的项目少于4个时仍不部署任何东西