ARM 模板,用于使用现有 VHD Uri (osDiskVHDUri) 在 azure 中创建 VM 并加入域

问题描述

用例描述

我有一个用例,其中我们需要使用存储帐户中可用的现有 VHD Uri 创建一个 azure 虚拟机,并且相同的 ARM 模板应该具有加入域的可行性。目前正在尝试工作和执行 ARM 模板,该模板仅具有使用现有 VHD Uri 和创建 VM 的灵活性(此 Uri 将充当“OsdiskVhdUri”),而另一个模板只能创建新 VM 并加入域这些都是独立工作的。

关键高亮:-

  • 需要一个同时包含“OsdiskVhdUri”和域加入参数的模板。
  • 模板参考应该是“OsdiskVhdUri”,因为当我尝试集成两个模板和故障排除时 - 部署时会出现与图像参考相关的错误
  • 非常重要的一点是 - ARM 模板,而蓝图分配要求 OsdiskVhdUri 参数,尽管我提供了用于创建 VM 的 Uri,但使用下面的模板,它似乎没有采用该 Uri”,而是创建了一个每次都有新的 VM 并附加到域。
  • 部署方法是 Azure 中的蓝图。

错误:- 由于以下错误,类型“模板”部署失败:模板部署失败,错误为 [{“消息”:“在类型为“ImageReference”的对象上找不到成员“osdiskVhdUri”。路径“properties.storageProfile.imageReference.osdiskVhdUri” ,第 1 行,位置 237。” }

用尽所有方法,找到更深入的研究,将不胜感激!

参考代码

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#","contentVersion": "1.0.0.0","parameters": {
        "existingVNETName": {
            "type": "string","Metadata": {
                "description": "Existing VNET that contains the domain controller"
            }
        },"osdiskVhdUri": {
            "type": "string","Metadata": {
                "description": "Uri of the existing VHD in ARM standard or premium storage"
            }
        },"osType": {
      "type": "string","defaultValue": "2019-Datacenter","allowedValues": [
        
        "2019-Datacenter"
        
                                
      ],"Metadata": {
        "description": "The Windows version for the VMs. Allowed values: 2008-R2-SP1,2012-Datacenter,2012-R2-Datacenter."
      }
    },"existingsubnetName": {
            "type": "string","Metadata": {
                "description": "Existing subnet that contains the domain controller"
            }
        },"vmname": {
            "type": "string","Metadata": {
                "description": "Unique public DNS prefix for the deployment. The fqdn will look something like '<dnsname>.westus.cloudapp.azure.com'. Up to 62 chars,digits or dashes,lowercase,should start with a letter: must conform to '^[a-z][a-z0-9-]{1,61}[a-z0-9]$'."
            }
        },"vmSize": {
            "type": "string","defaultValue": "Standard_D2_v2","Metadata": {
                "description": "The size of the virtual machines"
            }
        },"domainToJoin": {
            "type": "string","Metadata": {
                "description": "The FQDN of the AD domain"
            }
        },"domainUsername": {
            "type": "string","Metadata": {
                "description": "Username of the account on the domain"
            }
        },"domainPassword": {
            "type": "string","Metadata": {
                "description": "Password of the account on the domain"
            }
        },"ouPath": {
            "type": "string","defaultValue": "","Metadata": {
                "description": "Specifies an organizational unit (OU) for the domain account. Enter the full distinguished name of the OU in quotation marks. Example: \"OU=testOU; DC=domain; DC=Domain; DC=com\""
            }
        },"domainJoinoptions": {
            "type": "int","defaultValue": 3,"Metadata": {
                "description": "Set of bit flags that define the join options. Default value of 3 is a combination of NETSETUP_JOIN_DOMAIN (0x00000001) & NETSETUP_ACCT_CREATE (0x00000002) i.e. will join the domain and create the account on the domain. For more @R_100_4045@ion see https://msdn.microsoft.com/en-us/library/aa392154(v=vs.85).aspx"
            }
        },"vmAdminUsername": {
            "type": "string","Metadata": {
                "description": "The name of the administrator of the new VM and the domain. Exclusion list: 'admin','administrator"
            }
        },"vmAdminPassword": {
            "type": "string","Metadata": {
                "description": "The password for the administrator account of the new VM and the domain"
            }
        },"location": {
            "type": "string","defaultValue": "East US","Metadata": {
                "description": "Location for all resources."
            }
        }
    },"variables": {
        "storageAccountName": "[concat('diags',uniquestring(resourceGroup().id))]","diskName": "[concat('diags',"osdiskVhdUri": "[concat(parameters('osdiskVhdUri'),'-image')]","nicName": "[concat(parameters('vmname'),'Nic')]","publicIPName": "[concat(parameters('vmname'),'Pip')]","subnetId": "[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks/subnets',parameters('existingVNETName'),parameters('existingsubnetName'))]"
    },"resources": [
        {
            "apiVersion": "2015-06-15","type": "Microsoft.Network/publicIPAddresses","name": "[variables('publicIPName')]","location": "[parameters('location')]","properties": {
                "publicIPAllocationMethod": "Dynamic","dnsSettings": {
                    "domainNameLabel": "[parameters('vmname')]"
                }
            }
        },{
            "type": "Microsoft.Compute/disks","apiVersion": "2018-09-30","name": "[variables('diskName')]","properties": {
                "creationData": {
                    "createOption": "Import","sourceUri": "[parameters('osdiskVhdUri')]"
                }
            }
        },{
            "apiVersion": "2015-06-15","type": "Microsoft.Storage/storageAccounts","name": "[variables('storageAccountName')]","properties": {
                "accountType": "Standard_lrs"
            }
        },"type": "Microsoft.Network/networkInterfaces","name": "[variables('nicName')]","dependsOn": [
                "[concat('Microsoft.Network/publicIPAddresses/',variables('publicIPName'))]"
            ],"properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig","properties": {
                            "privateIPAllocationMethod": "Dynamic","publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPName'))]"
                            },"subnet": {
                                "id": "[variables('subnetId')]"
                            }
                        }
                    }
                ]
            }
        },{
      "type": "Microsoft.Compute/images","apiVersion": "2020-06-01","name": "[variables('imageName')]","properties": {
        "hyperVGeneration": "V2","storageProfile": {
          "osdisk": {
            "osType": "[parameters('osType')]","osstate": "Generalized","blobUri": "[parameters('osdiskVhdUri')]","caching": "ReadWrite","storageAccountType": "Standard_lrs"
          }
        }
      }
    },{
      "apiVersion": "2020-06-01","type": "Microsoft.Compute/virtualMachines","name": "[parameters('vmName')]","tags": {
        "displayName": "VirtualMachine"
      },"dependsOn": [
        "[variables('nicName')]","[variables('imageName')]"
      ],"properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },"osProfile": {
          "computerName": "[parameters('vmName')]","adminUsername": "[parameters('adminUsername')]","adminPassword": "[parameters('adminPasswordOrKey')]","linuxConfiguration": "[if(equals(parameters('authenticationType'),'password'),json('null'),variables('linuxConfiguration'))]"
        },"storageProfile": {
          "imageReference": {
              "id": "[resourceId('Microsoft.Compute/images',variables('imageName'))]"
          }
        },"networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
            }
          ]
        },"diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": true,"storageUri": "[reference(variables('diagStorageAccountName')).primaryEndpoints.blob]"
          }
        }
      }
    },"type": "Microsoft.Compute/virtualMachines/extensions","name": "[concat(parameters('vmname'),'/joindomain')]","dependsOn": [
                "[concat('Microsoft.Compute/virtualMachines/',parameters('vmname'))]"
            ],"properties": {
                "publisher": "Microsoft.Compute","type": "JsonADDomainExtension","typeHandlerVersion": "1.3","autoUpgradeMinorVersion": true,"settings": {
                    "Name": "[parameters('domainToJoin')]","OUPath": "[parameters('ouPath')]","User": "[concat(parameters('domainToJoin'),'\\',parameters('domainUsername'))]","Restart": "true","Options": "[parameters('domainJoinoptions')]"
                },"protectedSettings": {
                    "Password": "[parameters('domainPassword')]"
                }
            }
        }
    ]
}


解决方法

如果你想用 vhd 文件创建 Azure VM,请更新你的模板如下

{
      "type": "Microsoft.Compute/images","apiVersion": "2020-06-01","name": "[variables('imageName')]","location": "[parameters('location')]","properties": {
        "hyperVGeneration": "V2","storageProfile": {
          "osDisk": {
            "osType": "[parameters('osType')]","osState": "Generalized","blobUri": "[parameters('osDiskVhdUri')]","caching": "ReadWrite","storageAccountType": "Standard_LRS"
          }
        }
      }
    },{
      "apiVersion": "2020-06-01","type": "Microsoft.Compute/virtualMachines","name": "[parameters('vmName')]","tags": {
        "displayName": "VirtualMachine"
      },"dependsOn": [
        "[variables('nicName')]","[variables('imageName')]"
      ],"properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },"osProfile": {
          "computerName": "[parameters('vmName')]","adminUsername": "[parameters('adminUsername')]","adminPassword": "[parameters('adminPasswordOrKey')]","linuxConfiguration": "[if(equals(parameters('authenticationType'),'password'),json('null'),variables('linuxConfiguration'))]"
        },"storageProfile": {
          "imageReference": {
              "id": "[resourceId('Microsoft.Compute/images',variables('imageName'))]"
          }
        },"networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
            }
          ]
        },"diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": true,"storageUri": "[reference(variables('diagStorageAccountName')).primaryEndpoints.blob]"
          }
        }
      }
    }