使用ARM模板将现有受管磁盘附加到新VM

时间:2018-01-30 20:43:06

标签: azure arm-template

我想部署带有托管磁盘的虚拟机。我已经在另一个资源组中创建了一个托管磁盘,我想在模板部署期间使用它。我目前被封锁了。

我在这里使用下面的参数existingVirtualNetworkResourceGroup,这个托管磁盘我想在我的模板中使用它。

参数:

“existingVirtualNetworkResourceGroup”:{       “type”:“string”,       “defaultValue”:“poc-rg”,       “allowedValues”:[         “POC-RG”       ],

varibales:

"managedDisklocation":"[resourceId(parameters('existingVirtualNetworkResourceGroup'), 'Microsoft.Compute/disks')]",
"managedDiskpath":"[concat(variables(managedDisklocation),'/poc-manageddisk')]"

资源:

{
    "apiVersion": "2015-05-01-preview",
    "type": "Microsoft.Compute/virtualMachines",
    "name": "[variables('vmNameMdb')]",
    "location": "[resourceGroup().location]",
    "dependsOn": ["[concat('Microsoft.Storage/storageAccounts/', variables('newStorageAccountName'))]",
    "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"],
    "properties": {
        "hardwareProfile": {
            "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
            "computerName": "[variables('vmNameMdb')]",
            "adminUsername": "[parameters('adminUsername')]",
            "adminPassword": "[parameters('adminPassword')]",
            "secrets": [{
                "sourceVault": {
                    "id": "[parameters('keyVaultSubscriptionId')]"
                },
                "vaultCertificates": [{
                    "certificateUrl": "[parameters('engineCertificate')]"
                }],
                "vaultCertificates": [{
                    "certificateUrl": "[parameters('vmAgentCertificate')]"
                }]
            }]
        },
        "storageProfile": {
            "imageReference": {
                "publisher": "[parameters('imagePublisher')]",
                "offer": "[parameters('imageOffer')]",
                "sku": "[parameters('imageSKU')]",
                "version": "latest"
            },
            "osDisk": {
                "createOption": "FromImage"
            },
            "dataDisks": [
            {
                "lun": 0,
                "name": "[concat(parameters('vmName'),'-datadisk1')]",
                "createOption": "attach",
                "managedDisk": {
                        "id": "[parameters(managedDiskpath)]"
                }
            }
        ]
        },
        "networkProfile": {
            "networkInterfaces": [{
                "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
            }]
        }
    }
},

错误:

部署模板验证失败:'模板变量'managedDisklocation'无效:无法评估模板语言函数'resourceId':类型'Microsoft.Compute / disks'需要'1'资源名称参数。有关使用详情,请参阅https://aka.ms/arm-template-expressions/#resourceid ..有关使用详情,请参阅https://aka.ms/arm-template-expressions。 (代码:InvalidTemplate)

5 个答案:

答案 0 :(得分:0)

两个资源组都在同一个位置吗?

因为资源必须位于同一地区。

答案 1 :(得分:0)

你得到的错误显示了原因:

  

'模板变量' managedDisklocation'无效:无法执行   评估模板语言功能'

如果要将另一个磁盘附加到Azure VM,则磁盘的位置必须与原始磁盘的位置相同。这意味着VM和磁盘的位置应相同

答案 2 :(得分:0)

仔细查看代码,不确定为什么在'managedDiskpath'变量中有'managedDisklocation'。 'managedDiskpath'变量应如下所示,以映射到'poc-manageddisk'托管磁盘。

"managedDiskpath":"[resourceId('Microsoft.Compute/disks/poc-manageddisk')]"

答案 3 :(得分:0)

I was successfully able to able to deploy VM with attached disk from different resource group but same region with template below.

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "virtualMachineName": {
            "type": "String"
        },
        "virtualMachineSize": {
            "type": "String"
        },
        "adminUsername": {
            "type": "String"
        },
        "virtualNetworkName": {
            "type": "String"
        },
        "networkInterfaceName": {
            "type": "String"
        },
        "networkSecurityGroupName": {
            "type": "String"
        },
        "adminPassword": {
            "type": "SecureString"
        },
        "diagnosticsStorageAccountName": {
            "type": "String"
        },
        "publicIpAddressName": {
            "type": "String"
        },
        "manageddiskpath": {
            "type": "String"
        }
    },
    "variables": {
        "vnetId": "[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
        "subnetRef": "[concat(variables('vnetId'), '/subnets/','default')]"
    },
    "resources": [
        {
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[parameters('virtualMachineName')]",
            "apiVersion": "2016-04-30-preview",
            "location": "[resourceGroup().location]",
            "properties": {
                "osProfile": {
                    "computerName": "[parameters('virtualMachineName')]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]"
                },
                "hardwareProfile": {
                    "vmSize": "[parameters('virtualMachineSize')]"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "Canonical",
                        "offer": "UbuntuServer",
                        "sku": "16.04-LTS",
                        "version": "latest"
                    },
                    "osDisk": {
                        "createOption": "FromImage",
                        "managedDisk": {
                            "storageAccountType": "Standard_LRS"
                        }
                    },
                    "dataDisks": [
                        {
                            "lun": 0,
                            "createOption": "Attach",
                            "managedDisk": {
                                "id": "[parameters('manageddiskpath')]"
                            }
                        }
                    ]
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
                        }
                    ]
                },
                "diagnosticsProfile": {
                    "bootDiagnostics": {
                        "enabled": true,
                        "storageUri": "[reference(resourceId('Microsoft.Storage/storageAccounts', parameters('diagnosticsStorageAccountName')), '2015-06-15').primaryEndpoints['blob']]"
                    }
                }
            },
            "dependsOn": [
                "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
            ]
        },
        {
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[parameters('networkInterfaceName')]",
            "apiVersion": "2016-09-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            },
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIpAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
                            }
                        }
                    }
                ],
                "networkSecurityGroup": {
                    "id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]"
                }
            },
            "dependsOn": [
                "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]",
                "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]"
            ]
        },
        {
            "type": "Microsoft.Network/publicIpAddresses",
            "sku": {
                "name": "Basic"
            },
            "name": "[parameters('publicIpAddressName')]",
            "apiVersion": "2017-08-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "publicIpAllocationMethod": "Dynamic"
            }
        },
        {
            "type": "Microsoft.Network/networkSecurityGroups",
            "name": "[parameters('networkSecurityGroupName')]",
            "apiVersion": "2017-06-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "securityRules": [
                    {
                        "name": "default-allow-ssh",
                        "properties": {
                            "priority": 1000,
                            "protocol": "TCP",
                            "access": "Allow",
                            "direction": "Inbound",
                            "sourceAddressPrefix": "*",
                            "sourcePortRange": "*",
                            "destinationAddressPrefix": "*",
                            "destinationPortRange": "22"
                        }
                    }
                ]
            }
        }
    ],
    "outputs": {
        "adminUsername": {
            "type": "String",
            "value": "[parameters('adminUsername')]"
        }
    }
}

Key area, setting the ResourceID for the managed disk as a parameter, since its static and can't be generated.

enter image description here

enter image description here

enter image description here

enter image description here

Hope this helps.

答案 4 :(得分:0)

这里是创建新磁盘并将其附加到正在运行的现有虚拟机的代码。这样,您可以附加磁盘计数中提到的多个磁盘。

重要!!请确保虚拟机和您创建的磁盘属于相同的资源组位置

Template.json

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmPrefixName": {
            "type": "String"
        },
        "vmCount": {
            "defaultValue": 1,
            "type": "Int"
        },
        "diskStorageType": {
            "defaultValue": "StandardSSD_LRS",
            "allowedValues": [
                "StandardSSD_LRS",
                "Premium_LRS"
            ],
            "type": "String"
        },
        "dataDiskCount": {
            "defaultValue": 1,
            "type": "Int"
        },
        "dataDiskSize": {
            "defaultValue": [],
            "type": "Array"
        },
        "location": {
            "defaultValue": "[resourceGroup().location]",
            "type": "String"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2018-06-01",
            "name": "[concat(parameters('vmPrefixName'),string(if(greater(parameters('vmCount'),1),copyIndex(1),'')))]",
            "location": "[parameters('location')]",
            "properties": {
                "storageProfile": {
                    "copy": [
                        {
                            "name": "dataDisks",
                            "count": "[parameters('dataDiskCount')]",
                            "input": {
                                "name": "[concat(concat(parameters('vmPrefixName'),string(if(greater(parameters('vmCount'),1),copyIndex(1),''))),'_datadisk', string(add(copyIndex('dataDisks'),1)))]",
                                "lun": "[copyIndex('dataDisks')]",
                                "createOption": "Empty",
                                "diskSizeGB": "[parameters('dataDiskSize')[copyIndex('dataDisks')]]",
                                "managedDisk": {
                                    "storageAccountType": "[parameters('diskStorageType')]"
                                }
                            }
                        }
                    ]
                }
            }
        }
    ]
}

Parameter.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vmPrefixName": {
      "value": ""
    },
    "vmCount": {
      "value": 1
    },
    "diskStorageType": {
      "value": ""
    },
    "dataDiskCount": {
      "value": 2
    },
    "dataDiskSize": {
      "value": [
        128,
        64
      ]
    },
    "location": {
      "value": "eastus"
    }
  }
}