ARM 模板复制存储帐户和输出连接字符串

ARM template copy storage accounts and output connection strings

我需要部署 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": {}
}

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

那么几个选项:

  1. 您可以在 variables 部分使用复制来创建带复制的变量。
  2. 您可以创建部署循环以使用迭代器(有效地)构造变量
  3. 将存储帐户创建变成嵌套部署并执行 1 个部署 = 1 个存储帐户,这样您就无需费心复制到任何地方
  4. copy in outputs 可能已经可用了,很久没测试了

但我建议你退后一步,真的,大多数时候从 ARM 模板输出任何东西都是浪费精力,因为这些信息可以用 powershell 或 azure cli 以更少的努力来挽救。

这是一个基于您上面的示例模板修改的有效 ARM 模板。

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

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

输出:

{ "connectionstrings": [ { "connectionstring": "DefaultEndpointsProtocol=https;AccountName=0storageojjbpuu4wl6r4;AccountKey=" }, { "connectionstring": "DefaultEndpointsProtocol=https;AccountName=1storageojjbpuu4wl6r4;AccountKey=" }, { "connectionstring": "DefaultEndpointsProtocol=https;AccountName=2storageojjbpuu4wl6r4;AccountKey=" } ] }

{
  "$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')]"
    }
  }
}