ARM 範本中的輸出反覆運算

本文說明如何在 Azure Resource Manager 範本 (ARM 範本) 中為一個輸出建立多個值。 您可以藉由將複製迴圈新增至範本的輸出區段,在部署期間動態傳回數個項目。

您也可以搭配資源資源中的屬性變數使用複製迴圈。

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解,請參閱迴圈

語法

copy 元素新增至範本的輸出區段,以傳回數個項目。 copy 元素的一般格式如下:

"copy": {
  "count": <number-of-iterations>,
  "input": <values-for-the-output>
}

count 屬性可指定您希望輸出值反覆運算的次數。

input 屬性可指定您想要重複的屬性。 您可以建立一個由 input 屬性中的值建構的元素陣列。 其可以是單一屬性 (例如字串),或是具有數個屬性的物件。

複製限制

次數不能超過 800。

次數不可為負數。 如果您使用最新版的 Azure CLI、PowerShell 或 REST API 來部署範本,則其可為零。 具體而言,您必須使用:

  • Azure PowerShell 2.6 或更新版本
  • Azure CLI 2.0.74 或更新版本
  • Rest API 2019-05-10 版或更新版本
  • 針對部署資源類型,連結的部署必須使用 API 2019-05-10 版或更新版本

舊版 PowerShell、CLI 和 REST API 不支援次數為零。

輸出反覆運算

下列範例會建立數量可變的儲存體帳戶,並傳回每個儲存體帳戶的端點:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageCount": {
      "type": "int",
      "defaultValue": 2
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "variables": {
    "baseName": "[format('storage{0}', uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "copy": {
        "name": "storagecopy",
        "count": "[parameters('storageCount')]"
      },
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[format('{0}{1}', copyIndex(), variables('baseName'))]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    }
  ],
  "outputs": {
    "storageEndpoints": {
      "type": "array",
      "copy": {
        "count": "[parameters('storageCount')]",
        "input": "[reference(format('{0}{1}', copyIndex(), variables('baseName'))).primaryEndpoints.blob]"
      }
    }
  }
}

上述範本會傳回具有下列值的陣列:

[
  "https://0storagecfrbqnnmpeudi.blob.core.windows.net/",
  "https://1storagecfrbqnnmpeudi.blob.core.windows.net/"
]

下一個範例會從新的儲存體帳戶傳回三個屬性。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageCount": {
      "type": "int",
      "defaultValue": 2
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "variables": {
    "baseName": "[format('storage{0}', uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "copy": {
        "name": "storagecopy",
        "count": "[length(range(0, parameters('storageCount')))]"
      },
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[format('{0}{1}', range(0, parameters('storageCount'))[copyIndex()], variables('baseName'))]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    }
  ],
  "outputs": {
    "storageInfo": {
      "type": "array",
      "copy": {
        "count": "[length(range(0, parameters('storageCount')))]",
        "input": {
          "id": "[resourceId('Microsoft.Storage/storageAccounts', format('{0}{1}', copyIndex(), variables('baseName')))]",
          "blobEndpoint": "[reference(format('{0}{1}', copyIndex(), variables('baseName'))).primaryEndpoints.blob]",
          "status": "[reference(format('{0}{1}', copyIndex(), variables('baseName'))).statusOfPrimary]"
        }
      }
    }
  }
}

上述範例會傳回具有下列值的陣列:

[
  {
    "id": "/subscriptions/00000000/resourceGroups/demoGroup/providers/Microsoft.Storage/storageAccounts/0storagecfrbqnnmpeudi",
    "blobEndpoint": "https://0storagecfrbqnnmpeudi.blob.core.windows.net/",
    "status": "available"
  },
  {
    "id": "/subscriptions/00000000/resourceGroups/demoGroup/providers/Microsoft.Storage/storageAccounts/1storagecfrbqnnmpeudi",
    "blobEndpoint": "https://1storagecfrbqnnmpeudi.blob.core.windows.net/",
    "status": "available"
  }
]

下一步