Azure ARM JSON模板 - 将VM添加到不同资源组中的Recovery Services Vault

时间:2017-05-10 06:22:23

标签: json azure

我尝试添加将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

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

由于

3 个答案:

答案 0 :(得分:3)

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

我使用以下嵌套模板替换原始帖子中的恢复服务资源,恢复服务库所需的资源组由“resourceGroup”定义:“[parameters('nestedTemplateRecoveryServicesResourceGroup')]”,

<div class="original"></div>

答案 1 :(得分:1)

这里是我用来实现这个目标的JSON - 它在使用copyIndex()时进行扩展......如下面的资源所示。我已经在Azure门户上使用微软自己的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."
      }
    },
  },

答案 2 :(得分:0)

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