【问题标题】:Azure RM Template. Conditinally deploy Copy resourceAzure ARM 模板。有条件地部署复制资源
【发布时间】:2019-06-19 08:37:29
【问题描述】:

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

错误:代码=无效模板; 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)]"
      }
    }
  ]
}

【问题讨论】:

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


    【解决方案1】:

    尝试这样做:

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

    并设置你复制到这个:

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

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

    【讨论】:

    • 使用您的语法和 3 个条目,我得到:The template resource '[concat(parameters('apps')[copyIndex(4)].name,'-webtest')]' at line '31' and column '9' is not valid: The language expression property array index '4' is out of bounds 可以使用 5 个条目。
    • 啊,对,我认为创建嵌套部署和条件会更有意义(因此您不必急于解决这些问题)。或者您需要对使用该参数的每个表达式设置条件。
    • 我想我确实会使用嵌套部署。您能否举个例子,我如何“对使用该参数的每个表达式设置条件。”?我也想知道为什么"condition": "[greater(length(parameters('apps')),4)]", 会被忽略?确切的这种情况的文档说“如果您需要指定是否完全部署资源,请参阅条件元素。” docs.microsoft.com/en-us/azure/azure-resource-manager/… 我相信这就是我们正在做的事情。
    • 它并没有真正被忽略。 arm模板分为2个步骤。编译和实施。您的模板无法编译。条件仅在实施步骤中得到评估。不幸的是,条件在这里无法为您提供帮助。
    • 我最终在每个使用 copyIndex 的参数上都使用了 if 函数。这就是您可能的意思:“您需要对使用该参数的每个表达式设置条件”。 "count": "[if(greater(length(parameters('apps')),4), sub(length(parameters('apps')),4), 1)]" 是关键。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多