我需要部署 N 个存储帐户并将连接字符串输出为数组或更好的逗号分隔的统一字符串值。我发现了一个关于如何部署多个资源的非常有用的 article。这是我如何创建多个存储帐户。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "apiVersion": "2016-01-01",
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[concat(copyIndex(),'storage', uniqueString(resourceGroup().id))]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {},
            "copy": {
                "name": "storagecopy",
                "count": 3
            }
        }
    ],
    "outputs": {}
}

现在的问题是,没有关于如何遍历存储帐户以输出连接字符串的信息。有没有人做过这样的事情?如何遍历已部署的存储帐户并输出连接字符串?

最佳答案

这是基于您上面的示例模板修改的工作 ARM 模板。

它能够在部署输出中输出通过 ARM 模板部署部署的 部分 存储帐户连接字符串列表, 没有存储帐户 key

这是由于一个公开的已知问题:ARM 中的 listKeys not supported in variable #1503,其中不允许在 ARM 模板变量中使用用于列出存储帐户 key 的 listKeys

输出:


{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountCount": {
      "type": "int",
      "defaultValue": 3
    }
  },
  "variables": {
    "storageAccountConnectionStrings": {
      "copy": [
        {
          "name": "connectionstrings",
          "count": "[parameters('storageAccountCount')]",
          "input": {
            "connectionstring": "[concat('DefaultEndpointsProtocol=https;AccountName=', concat(copyIndex('connectionstrings'),'storage', uniqueString(resourceGroup().id)), ';AccountKey=')]"
          }
        }
      ]
    }
  },
  "resources": [
    {
      "apiVersion": "2016-01-01",
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[concat(copyIndex(),'storage', uniqueString(resourceGroup().id))]",
      "location": "[resourceGroup().location]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "copy": {
        "name": "storagecopy",
        "count": "[parameters('storageAccountCount')]"
      }
    }
  ],
  "outputs": {
    "connectionStringsArray": {
      "type": "object",
      "value": "[variables('storageAccountConnectionStrings')]"
    }
  }
}

关于azure - ARM 模板复制存储帐户并输出连接字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48756953/

10-11 07:47