0

Use-case description:

I've a use case wherein, we need to create a VM in azure using the existing VHD Uri available in storage account & the same ARM template should have the feasibility to join to domain. At the present currently tried working & executing the ARM template which has only flexibility of using the existing VHD Uri and creating a VM (this Uri will act as "OsDiskVhdUri") & another template has only the ability to create new VM and join to domain these are working on standalone basis.

Key highlighters:-

  • Need a template which has both "OsDiskVhdUri" & domain join parameters.
  • Template reference should be "OsDiskVhdUri", because when i tried integrating both templates & troubleshooting - Image reference related errors are there on while deployment.
  • The very important point, was - the ARM template while blueprint assignment asks for OsDiskVhdUri parameter and although I give the Uri for creating the VM, with the below template, it doesn't seems to take that Uri" instead it creates a NEW VM every time and attaches to domain.
  • Deployment method is blueprint in Azure.

Error:- type 'Template' failed to deploy due to the following error: Template deployment failed with error [ { "message": "Could not find member 'osDiskVhdUri' on object of type 'ImageReference'. Path 'properties.storageProfile.imageReference.osDiskVhdUri', line 1, position 237." }

Exhausted all methods finding still deeper dive into it & any guidance on this will be highly appreciated!!

Code for reference:

{
    "$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 information 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', uniquestring(resourceGroup().id))]",
        "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')]",
            "location": "[parameters('location')]",
            "properties": {
                "creationData": {
                    "createOption": "Import",
                    "sourceUri": "[parameters('osDiskVhdUri')]"
                }
            }
        },
        {
            "apiVersion": "2015-06-15",
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[variables('storageAccountName')]",
            "location": "[parameters('location')]",
            "properties": {
                "accountType": "Standard_LRS"
            }
        },
        {
            "apiVersion": "2015-06-15",
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[variables('nicName')]",
            "location": "[parameters('location')]",
            "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')]",
      "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')]",
      "location": "[parameters('location')]",
      "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]"
          }
        }
      }
    },
        {
            "apiVersion": "2015-06-15",
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "name": "[concat(parameters('vmname'),'/joindomain')]",
            "location": "[parameters('location')]",
            "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')]"
                }
            }
        }
    ]
}


1 Answers1

1

If you want to create Azure VM with vhd file, please update your template as below

{
      "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')]",
      "location": "[parameters('location')]",
      "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]"
          }
        }
      }
    }
Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • Thank you Jim, Will try and let you know in sometime and share next updates. – Anirudh Bragadeesan May 26 '21 at 07:55
  • Worked on the shared template & tried using the same given, But it had a problem on variable section during deployment referring this error in blueprints, So quizzed myself what we need modify based on this. Since now in new template which we used executing that is having "imageName" which is not refered on top Can you please hlp on that - "Deployment template validation failed: 'The template variable 'imageName' is not found. Please see https://aka.ms/arm-template/#variables for usage details." – Anirudh Bragadeesan May 26 '21 at 09:43
  • Jim, I think if that part is cleared then our solution is nearer. – Anirudh Bragadeesan May 26 '21 at 09:57
  • @AnirudhBragadeesan Do you define `imageName` in variables? – Jim Xu May 27 '21 at 01:06
  • am not sure. am still trying ..because the deployment error relates to it ..am not able to identify anything else. rest all seems to be good. If you can please help me,kindly – Anirudh Bragadeesan May 27 '21 at 05:13
  • @AnirudhBragadeesan Could you please provide your template? – Jim Xu May 27 '21 at 05:30
  • Thank you for your response. Edited & Updated the Template in my original post itself (completely) Can you pls have a check & guide me. – Anirudh Bragadeesan May 27 '21 at 05:40
  • @AnirudhBragadeesan According to your template, you do not define `imageName` in variables. please update variables as `"variables": { "storageAccountName": "[concat('diags', uniquestring(resourceGroup().id))]", "imageName":"", ....} ` – Jim Xu May 27 '21 at 05:43
  • Thanks @Jim, I understand from above comments that for "imageName" we should give value as that of storageAccountName - "[concat('diags', uniquestring(resourceGroup().id))]". But whent tried, the deployment still fails as below error. Deployment template validation failed: 'The template variable 'diagStorageAccountName' is not found. Can we get into a chat discussion(pls provide link) if your fine & guide me it would grateful. – Anirudh Bragadeesan May 27 '21 at 05:54
  • @AnirudhBragadeesan you can update the template as `"diagnosticsProfile": { "bootDiagnostics": { "enabled": true } }`. It will not affect anything. For more details, please refer to https://learn.microsoft.com/en-us/azure/virtual-machines/boot-diagnostics – Jim Xu May 27 '21 at 06:01
  • Thank you @jim.that worked for the last error but still the template has some minor issue on this - below deployment error - Pls kindly guide. Not sure what we are missing. Template deployment failed with error [ { "code": "BadRequest", "message": "Error converting value \"2019-Datacenter\" to type 'Microsoft.WindowsAzure.ComputeResourceProvider.API.OSType'. Path 'properties.storageProfile.osDisk.osType', line 1, position 114." } ] – Anirudh Bragadeesan May 27 '21 at 06:09
  • @AnirudhBragadeesan Please update template as `"osType": { "type": "string", "allowedValues": [ "Windows", "Linux" ], "defaultValue": "Windows", "metadata": { "description": "Type of OS on the existing vhd" } }` – Jim Xu May 27 '21 at 06:19
  • Thanks Jim a lot!! We will soon confirm on the further progress i think we are working very closer to our issues. Really it helped a lot from your inputs. Testing in progress. Really appreciated you valuable time as well. – Anirudh Bragadeesan May 27 '21 at 07:31
  • The template helped in creating the VM, using existing VHD Uri & VM gets spinned on the portal , But the RDP connectivity isn't seems to be fine as some issues on Hypervisor related. Deployment on template went for long & gave below error (even though vm was created), on other note if see boot diag on portal that as well seems not good & size of vm. error shared in next comments. But if temp changed to V1 (gen) then fails within seconds – Anirudh Bragadeesan May 27 '21 at 09:08
  • error for prev comments "Cannot modify extensions in the VM when the VM is not running. The artifact 'f3821ec4-1091-4032-9f03-f00a2bf80799' of type 'Template' failed to deploy due to the following error: Template deployment failed with error "code": "BadRequest", "message": "The selected VM size 'Standard_D2_v2' cannot boot Hypervisor Generation '2'. If this was a Create operation please check that the Hypervisor Generation of the Image matches the Hypervisor Generation of the selected VM Size. If this was an Update operation please select a Hypervisor Generation '2' VM Size." } ] – Anirudh Bragadeesan May 27 '21 at 09:09
  • @AnirudhBragadeesan please refer to https://learn.microsoft.com/en-us/azure/virtual-machines/generation-2 – Jim Xu May 27 '21 at 09:13
  • some prob on Sysprep is what i understand..further testing it – Anirudh Bragadeesan May 27 '21 at 15:44
  • Vm booting seems to be problem althought ARM template spins up the vm using our vhd uri (image) – Anirudh Bragadeesan May 28 '21 at 06:12