Azure ARM JSON模板-将VM添加到不同资源组中的恢复服务Vault

Azure ARM JSON模板-将VM添加到不同资源组中的恢复服务Vault,json,azure,Json,Azure,我正在尝试将VM添加到恢复服务保险库的功能添加到我用于部署的现有Azure ARM JSON模板中 我使用了以下GitHub模板中的代码,如果recovery services vault与VM位于同一资源组中,但如果它位于不同的资源组中,则此方法不起作用 代码如下: { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion":

我正在尝试将VM添加到恢复服务保险库的功能添加到我用于部署的现有Azure ARM JSON模板中

我使用了以下GitHub模板中的代码,如果recovery services vault与VM位于同一资源组中,但如果它位于不同的资源组中,则此方法不起作用

代码如下:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "existingVirtualMachinesResourceGroup": {
        "type": "string",
        "metadata": {
            "description": "Resource group where the virtual machines are located. This can be different than resource group of the vault. "
        }
    },
    "existingVirtualMachines": {
        "type": "array",
        "metadata": {
            "description": "Array of Azure virtual machines. e.g. [\"vm1\",\"vm2\",\"vm3\"]"
        }
    },
    "existingRecoveryServicesVault": {
        "type": "string",
        "metadata": {
            "description": "Recovery services vault name where the VMs will be backed up to. "
        }
    },
    "existingBackupPolicy": {
        "type": "string",
        "defaultValue": "DefaultPolicy",
        "metadata": {
            "description": "Backup policy to be used to backup VMs. Backup POlicy defines the schedule of the backup and how long to retain backup copies. By default every vault comes with a 'DefaultPolicy' which canbe used here."
        }
    }
},
"variables": {
    "backupFabric": "Azure",
    "v2VmType": "Microsoft.Compute/virtualMachines",
    "v2VmContainer": "iaasvmcontainer;iaasvmcontainerv2;",
    "v2Vm": "vm;iaasvmcontainerv2;"
},
"resources": [
    {
        "name": "[concat(parameters('existingRecoveryServicesVault'), '/', variables('backupFabric'), '/', variables('v2VmContainer'), concat(parameters('existingVirtualMachinesResourceGroup'),';',parameters('existingVirtualMachines')[copyIndex()]), '/', variables('v2Vm'), concat(parameters('existingVirtualMachinesResourceGroup'),';',parameters('existingVirtualMachines')[copyIndex()]))]",
        "apiVersion": "2016-06-01",
        "location": "[resourceGroup().location]",
        "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems",
        "copy": {
            "name": "v2VmsCopy",
            "count": "[length(parameters('existingVirtualMachines'))]"
        },
        "properties": {
            "protectedItemType": "[variables('v2VmType')]",
            "policyId": "[resourceId('Microsoft.RecoveryServices/vaults/backupPolicies',parameters('existingRecoveryServicesVault'),parameters('existingBackupPolicy') )]",
            "sourceResourceId": "[resourceId(subscription().subscriptionId,parameters('existingVirtualMachinesResourceGroup'),'Microsoft.Compute/virtualMachines',parameters('existingVirtualMachines')[copyIndex()])]"
        }
    }
]
}

没有用于定义恢复服务Vault资源组的变量或参数

我还研究了以下GitHub模板,该模板还向恢复服务保险库添加了一个VM,但它似乎无法使用不同的资源组

我尝试过谷歌搜索,但到目前为止我还没有找到答案,所以有可能吗


谢谢

如果对其他人有用的话,我已经找到了答案。如前所述,恢复服务vault将使用为模板定义的相同资源组。为了能够为RSV定义不同的模板,需要使用嵌套模板

我已使用以下嵌套模板替换了我的原始帖子中的recovery services资源,recovery services vault所需的资源组由“resourceGroup”定义“[parameters('nestedTemplateRecoveryServicesResourceGroup')”


下面是我用来实现这一点的JSON—它在使用copyIndex()时会伸缩,如下面的参考资料所示。我从Microsoft自己在Azure门户上的JSON模板中修改了这一点,因为他们改进了VM部署,在部署期间包含了备份选项

我通常提供一个VM名称数组,并使用复制循环从这些名称中命名其他资源。从技术上讲,它们在构建时与VM相关或是VM的子级,因此使用VM名称作为相关资源名称的基础似乎最为合适。我使用copyIndex()来输入在该循环中迭代的VM的名称(索引),这意味着所有子资源和嵌套模板也可以使用这些参数

无论如何,这就是资源(一个嵌套的模板,您必须这样做)。下面是相关变量和参数

{
  "apiVersion": "2017-05-10",
  "name": "[concat(parameters('virtualMachineNames')[copyIndex()], '-' , 'BackupIntent')]",
  "type": "Microsoft.Resources/deployments",
  "resourceGroup": "[parameters('DestinationRSVResourceGroup')]",
  "copy": {
    "name": "AzureBackupLoop",
    "count": "[length(parameters('virtualMachineNames'))]"
  },
  "dependsOn": [
    "NameOfPreviousLoop"
  ],
  "properties": {
    "mode": "Incremental",
    "template": {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "resources": [
        {
          "name": "[concat(parameters('DestinationRecoveryServicesVault'), '/', 'Azure', '/', variables('v2Vm'), resourceGroup().name, ';', parameters('virtualMachineNames')[copyIndex()])]",
          "apiVersion": "2017-07-01",
          "type": "Microsoft.RecoveryServices/vaults/backupFabrics/backupProtectionIntent",
          "properties": {
            "friendlyName": "[concat(parameters('virtualMachineNames')[copyIndex()], 'BackupIntent')]",
            "protectionIntentItemType": "AzureResourceItem",
            "policyId": "[resourceId(parameters('DestinationRSVResourceGroup'), 'Microsoft.RecoveryServices/vaults/backupPolicies', parameters('DestinationRecoveryServicesVault'), parameters('existingBackupPolicy'))]",
            "sourceResourceId": "[resourceId(resourceGroup().name, 'Microsoft.Compute/virtualMachines', parameters('virtualMachineNames')[copyIndex()])]"
          }
        }
      ]
    }
  }
}
此模板中的变量部分如下所示:

  "variables": {
    "v2Vm": "vm;iaasvmcontainerv2;",
  },
最后是参数(与此资源相关):


如果要使用该嵌套模板,请确保将在第一个模板中设置的VM添加为DependsOn。这样,当它运行增量部署时,虚拟机已经存在。

如果对其他人有用,我已经找到了答案。如前所述,恢复服务vault将使用为模板定义的相同资源组。为了能够为RSV定义不同的模板,这需要使用嵌套模板完成。不要假设您在需要copyIndex的多个VM部署中使用了此模板?而不是在多个VM部署中使用此模板,我已经在多个数据磁盘上使用了它,除非您选择了0个错误磁盘。您看过关于部署多个VM的帖子吗,不知道这是否是您想要的?是的,我有,并使用该指南为copyIndex创建了一些测试VM。其实我就是在这个基础上提出了一个问题,就是以他自己的网页上面的这个问题为指导,;仅供参考,我已将问题分类,您可以在此处查看:
  "variables": {
    "v2Vm": "vm;iaasvmcontainerv2;",
  },
"parameters": {
    "DestinationRecoveryServicesVault": {
      "type": "string",
      "metadata": {
        "description": "Name of the recovery services vault that the VM is to be backed up in to."
      }
    },
    "existingBackupPolicy": {
      "type": "string",
      "metadata": {
        "description": "Name of the backup policy that the VM will use."
      }
    },
    "DestinationRSVResourceGroup": {
      "type": "string",
      "metadata": {
        "description": "Resource group of the RSV."
      }
    }
    "virtualMachineNames": {
      "type": "array",
      "metadata": {
        "description": "An array of names for the VMs."
      }
    },
  },