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



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

我使用了以下 GitHub 模板中的代码,如果恢复服务保管库与 VM 位于同一资源组中,则此方法有效,但如果它位于不同的资源组中,则此方法无效。

https://github.com/Azure/azure-quickstart-templates/tree/master/101-recovery-services-backup-vms

代码如下:

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

}

没有用于定义恢复服务保管库资源组的变量或参数。

我还查看了以下 GitHub 模板,该模板还将 VM 添加到恢复服务保管库,但这似乎无法使用不同的资源组。

https://github.com/Azure/azure-quickstart-templates/tree/master/201-recovery-services-backup-classic-resource-manager-vms

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

谢谢

如果它对其他人有用,我已经找到了答案。如前所述,恢复服务保管库将使用为模板定义的同一资源组。为了能够为 RSV 定义不同的模板,这需要使用嵌套模板来完成。

我在原始帖子中使用以下嵌套模板来替换恢复服务资源,恢复服务保管库所需的资源组由"资源组"定义:"[参数('嵌套模板恢复服务资源组')]",

{
"apiVersion": "2017-05-10",
"name": "nestedTemplateRecoveryServices",
"type": "Microsoft.Resources/deployments",
"resourceGroup": "[parameters('nestedTemplateRecoveryServicesResourceGroup')]",
"dependsOn": ["[concat('Microsoft.Compute/virtualMachines/', parameters('vmName'))]"],
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [
{
"name": "[concat(parameters('existingRecoveryServicesVault'), '/', variables('backupFabric'), '/', variables('v2VmContainer'), concat(parameters('existingVirtualMachinesResourceGroup'),';',parameters('existingVirtualMachines')), '/', variables('v2Vm'), concat(parameters('existingVirtualMachinesResourceGroup'),';',parameters('existingVirtualMachines')))]",
"apiVersion": "2016-06-01",
"location": "[resourceGroup().location]",
"type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems",
"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'))]"
}
}
]
},
"parameters": {},
"outputs": {}
}
}

这是我用来实现这一点的 JSON - 它在使用 copyIndex()时可扩展...包含在下面的资源中。我根据 Azure 门户上Microsoft自己的 JSON 模板改编了此模板,因为他们改进了 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;",
},

最后是参数(与此资源相关):

"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."
}
},
},

如果要使用该嵌套模板,请确保将在第一个模板中预配的 VM 添加为依赖项。 这样,当它运行增量部署时,VM 已经存在。

最新更新