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

Azure ARM模板复制存储帐户和输出连接字符串,azure,azure-resource-manager,arm-template,Azure,Azure Resource Manager,Arm Template,我需要部署N个存储帐户,并将连接字符串输出为数组或更好的逗号分隔统一字符串值。我发现了一个关于如何部署多个资源的非常有用的方法。下面是我如何创建多个存储帐户的方法 { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [ {

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

{
    "$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": {}
}
现在的问题是,没有关于如何迭代存储帐户以输出连接字符串的信息。有人做过这样的事吗?如何遍历已部署的存储帐户和输出连接字符串?

因此有几个选项:

  • 您可以使用copy inside
    variables
    部分来创建带有copy的变量
  • 您可以创建一个部署循环来使用迭代器(有效地)构造变量
  • 将存储帐户创建转换为嵌套部署,并执行1个部署=1个存储帐户,这样您就不需要在任何地方复制了
  • 复制输出可能已经可用,但很长时间没有进行测试

  • 但我建议您退后一步,实际上,大多数时候从ARM模板输出任何内容都是浪费精力,因为使用powershell或azure cli可以轻松地回收这些信息。

    这是一个基于上述示例模板修改的ARM工作模板

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

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

    输出:

    {“连接字符串”:[ { “connectionstring”:“DefaultEndpointsProtocol=https;AccountName=0StorageOJJBPU4WL6R4;AccountKey=” }, { “connectionstring”:“DefaultEndpointsProtocol=https;AccountName=1Storage OJJBPU4WL6R4;AccountKey=” }, { “connectionstring”:“DefaultEndpointsProtocol=https;AccountName=2StorageOJJBPU4WL6R4;AccountKey=” }]}


    我有类似的ARM模板,遇到了相同的问题。我认为输出部分不支持复制迭代器。你能分享更多关于你的场景的细节吗?为什么需要输出连接字符串?是的,在实际创建资源之前不能真正使用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')]"
        }
      }
    }