0

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

错误:代码=无效模板;消息=部署模板验证失败:“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)]"
      }
    }
  ]
}
4

1 回答 1

1

尝试这样做:

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

并将您复制到此:

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

这应该解决这样一个事实,即在您的情况下计数为负并且当您的数组中的项目少于 4 个时仍然不部署任何东西

于 2019-06-19T09:38:39.850 回答