Search code examples
azurepowershellazure-resource-managerazure-virtual-machine

New-AzResourceGroupDeployment fails due to indexing in IP adress value when creating


New-AzResourceGroupDeployment fails when deploying a virtual network via ARM template. It seems like the ARM template is iterating through one single IP adress by seeing it's dots ('.') as delimiter:

Errormessage:

New-AzResourceGroupDeployment : 16:00:26 - The deployment 'vm2' failed with error(s). Showing 1 out of 1 error(s).
Status Message: Cannot parse the request. (Code: InvalidRequestFormat)
 - Unexpected character encountered while parsing value: {. Path 'properties.addressSpace.addressPrefixes', line 1, position 133. (Code:InvalidJson)
 - After parsing a value an unexpected character was encountered: :. Path 'properties.addressSpace.addressPrefixes[0]', line 1, position 145. (Code:InvalidJson)
 - After parsing a value an unexpected character was encountered: :. Path 'properties.addressSpace.addressPrefixes[0]', line 1, position 145. (Code:InvalidJson)
 - After parsing a value an unexpected character was encountered: :. Path 'properties.addressSpace.addressPrefixes[0]', line 1, position 145. (Code:InvalidJson)
 - After parsing a value an unexpected character was encountered: :. Path 'properties.addressSpace.addressPrefixes[0]', line 1, position 145. (Code:InvalidJson)
CorrelationId: 5cf0698f-1f96-4adf-a242-312ded6bc9fe
At line:1 char:1
+ New-AzResourceGroupDeployment -ResourceGroupName "clientname-rg-vm-01 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-AzResourceGroupDeployment], Exception
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

The part of the template file looks like this:

   ..."parameters": {
        "addressPrefixes": {
          "type": "array"
         },
        "subnets": {
          "type": "array"
        }
      },...

        ..."properties": {
               "addressSpace": {
                   "addressPrefixes": "[parameters('addressPrefixes')]"
               },
               "subnets": "[parameters('subnets')]"
           },...

The full template file looks like this:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "environment": {
            "type": "string",
            "metadata": {
                "description": "Omgeving Dev, Tst, Acc, Prd"
            }
        },
        "companyName": {
            "type": "string"
        },
        "resourceType": {
            "type": "string"
        },
        "sequenceNumber": {
            "type": "string"
        },
        "location": {
            "type": "string"
        },
        "networkInterfaceName": {
            "type": "string"
        },
        "enableAcceleratedNetworking": {
            "type": "bool"
        },
        "networkSecurityGroupName": {
            "type": "string"
        },
        "networkSecurityGroupRules": {
            "type": "array"
        },
        "subnetName": {
            "type": "string"
        },
        "virtualNetworkName": {
            "type": "string"
        },
        "addressPrefixes": {
            "type": "array"
        },
        "subnets": {
            "type": "array"
        },
        "publicIpAddressName": {
            "type": "string"
        },
        "publicIpAddressType": {
            "type": "string"
        },
        "publicIpAddressSku": {
            "type": "string"
        },
        "pipDeleteOption": {
            "type": "string"
        },
        "virtualMachineRG": {
            "type": "string"
        },
        "osDiskType": {
            "type": "string"
        },
        "osDiskDeleteOption": {
            "type": "string"
        },
        "virtualMachineSize": {
            "type": "string"
        },
        "nicDeleteOption": {
            "type": "string"
        },
        "adminUsername": {
            "type": "string"
        },
        "adminPassword": {
            "type": "secureString"
        },
        "patchMode": {
            "type": "string"
        },
        "enableHotpatching": {
            "type": "bool"
        },
        "tags": {
            "type": "object"
        }
    },
    "variables": {
        "vmName": "[concat(parameters('companyName'), parameters('resourceType'), parameters('sequenceNumber'), parameters('environment'))]",
        "nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]",
        "vnetName": "[parameters('virtualNetworkName')]",
        "vnetId": "[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
        "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
    },
    "resources": [
        {
            "name": "[parameters('networkInterfaceName')]",
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2021-03-01",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
                "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
                "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            },
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIpAddress": {
                                "id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]",
                                "properties": {
                                    "deleteOption": "[parameters('pipDeleteOption')]"
                                }
                            }
                        }
                    }
                ],
                "enableAcceleratedNetworking": "[parameters('enableAcceleratedNetworking')]",
                "networkSecurityGroup": {
                    "id": "[variables('nsgId')]"
                }
            },
            "tags": "[parameters('tags')]"
        },
        {
            "name": "[parameters('networkSecurityGroupName')]",
            "type": "Microsoft.Network/networkSecurityGroups",
            "apiVersion": "2019-02-01",
            "location": "[parameters('location')]",
            "properties": {
                "securityRules": "[parameters('networkSecurityGroupRules')]"
            },
            "tags": "[parameters('tags')]"
        },
        {
            "name": "[parameters('virtualNetworkName')]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-11-01",
            "location": "[parameters('location')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": "[parameters('addressPrefixes')]"
                },
                "subnets": "[parameters('subnets')]"
            },
            "tags": "[parameters('tags')]"
        },
        {
            "name": "[parameters('publicIpAddressName')]",
            "type": "Microsoft.Network/publicIpAddresses",
            "apiVersion": "2019-02-01",
            "location": "[parameters('location')]",
            "properties": {
                "publicIpAllocationMethod": "[parameters('publicIpAddressType')]"
            },
            "sku": {
                "name": "[parameters('publicIpAddressSku')]"
            },
            "tags": "[parameters('tags')]"
        },
        {
            "name": "[variables('vmName')]",
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2021-07-01",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
            ],
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('virtualMachineSize')]"
                },
                "storageProfile": {
                    "osDisk": {
                        "createOption": "fromImage",
                        "managedDisk": {
                            "storageAccountType": "[parameters('osDiskType')]"
                        },
                        "deleteOption": "[parameters('osDiskDeleteOption')]"
                    },
                    "imageReference": {
                        "publisher": "MicrosoftWindowsServer",
                        "offer": "WindowsServer",
                        "sku": "2019-datacenter-gensecond",
                        "version": "latest"
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]",
                            "properties": {
                                "deleteOption": "[parameters('nicDeleteOption')]"
                            }
                        }
                    ]
                },
                "osProfile": {
                    "computerName": "[variables('vmName')]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]",
                    "windowsConfiguration": {
                        "enableAutomaticUpdates": true,
                        "provisionVmAgent": true,
                        "patchSettings": {
                            "enableHotpatching": "[parameters('enableHotpatching')]",
                            "patchMode": "[parameters('patchMode')]"
                        }
                    }
                },
                "licenseType": "Windows_Server"
            },
            "tags": "[parameters('tags')]"
        }
    ],
    "outputs": {
        "adminUsername": {
            "type": "string",
            "value": "[parameters('adminUsername')]"
        }
    }
}

The part of the ARM parameter file looks like this:

   ..."addressPrefixes": {
           "value": [
               "172.50.28.0/22"
           ]
       },
       "subnets": {
           "value": [
               {
                   "name": "default_VM01",
                   "properties": {
                       "addressPrefix": "172.50.28.0/26"
                   }
               }
           ]
       },...

Modules versions

Does anyone know how to solve this problem?


Solution

  • We have tried the same to create virtual machine along with virtual network as mentioned above and it works fine.

    Please make sure that the address prefix section you provided is in correct format and you have updated az cli/ powershell versions in your environment.

    You can use the below template by providng your creadentials to do the same operation ;

    Template.json

    {
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    "location": {
    "type": "string"
    },
    "networkInterfaceName": {
    "type": "string"
    },
    "enableAcceleratedNetworking": {
    "type": "bool"
    },
    "networkSecurityGroupName": {
    "type": "string"
    },
    "networkSecurityGroupRules": {
    "type": "array"
    },
    "subnetName": {
    "type": "string"
    },
    "virtualNetworkName": {
    "type": "string"
    },
    "addressPrefixes": {
    "type": "array"
    },
    "subnets": {
    "type": "array"
    },
    "publicIpAddressName": {
    "type": "string"
    },
    "publicIpAddressType": {
    "type": "string"
    },
    "publicIpAddressSku": {
    "type": "string"
    },
    "pipDeleteOption": {
    "type": "string"
    },
    "virtualMachineName": {
    "type": "string"
    },
    
    "virtualMachineComputerName": {
    "type": "string"
    },
    "virtualMachineRG": {
    "type": "string"
    },
    "osDiskType": {
    "type": "string"
    },
    "osDiskDeleteOption": {
    "type": "string"
    },
    "virtualMachineSize": {
    "type": "string"
    },
    "nicDeleteOption": {
    "type": "string"
    },
    "adminUsername": {
    "type": "string"
    },
    "adminPassword": {
    "type": "secureString"
    },
    "patchMode": {
    "type": "string"
    },
    "enableHotpatching": {
    "type": "bool"
    },
    "tags": {
     "type": "object"
     }
     },
    "variables": {
                                  "nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]",
                                  "vnetName": "[parameters('virtualNetworkName')]",
                                  "vnetId": "[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
                                  "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
                          
    },
                                  "resources": [
                                      {
                                          "name": "[parameters('networkInterfaceName')]",
                                          "type": "Microsoft.Network/networkInterfaces",
                                          "apiVersion": "2021-03-01",
                                          "location": "[parameters('location')]",
                                          "dependsOn": [
                                              "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
                                              "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
                                              "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
                                          ],
                                          "properties": {
                                              "ipConfigurations": [
                                                  {
                                                      "name": "ipconfig1",
                                                      "properties": {
                                                          "subnet": {
                                                              "id": "[variables('subnetRef')]"
                                                          },
                                                          "privateIPAllocationMethod": "Dynamic",
                                                          "publicIpAddress": {
                                                              "id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]",
                                                              "properties": {
                                                                  "deleteOption": "[parameters('pipDeleteOption')]"
                                                              }
                                                          }
                                                      }
                                                  }
                                              ],
                                              "enableAcceleratedNetworking": "[parameters('enableAcceleratedNetworking')]",
                                              "networkSecurityGroup": {
                                                  "id": "[variables('nsgId')]"
                                              }
                                          },
                                          "tags": "[parameters('tags')]"
                                      },
                                      {
                                          "name": "[parameters('networkSecurityGroupName')]",
                                          "type": "Microsoft.Network/networkSecurityGroups",
                                          "apiVersion": "2019-02-01",
                                          "location": "[parameters('location')]",
                                          "properties": {
                                              "securityRules": "[parameters('networkSecurityGroupRules')]"
                                          },
                                          "tags": "[parameters('tags')]"
                                      },
                                      {
                                          "name": "[parameters('virtualNetworkName')]",
                                          "type": "Microsoft.Network/virtualNetworks",
                                          "apiVersion": "2020-11-01",
                                          "location": "[parameters('location')]",
                                          "properties": {
                                              "addressSpace": {
                                                  "addressPrefixes": "[parameters('addressPrefixes')]"
                                              },
                                              "subnets": "[parameters('subnets')]"
                                          },
                                          "tags": "[parameters('tags')]"
                                      },
                                      {
                                          "name": "[parameters('publicIpAddressName')]",
                                          "type": "Microsoft.Network/publicIpAddresses",
                                          "apiVersion": "2019-02-01",
                                          "location": "[parameters('location')]",
                                          "properties": {
                                              "publicIpAllocationMethod": "[parameters('publicIpAddressType')]"
                                          },
                                          "sku": {
                                              "name": "[parameters('publicIpAddressSku')]"
                                          },
                                          "tags": "[parameters('tags')]"
                                      },
                                      {
                                          "name": "[parameters('virtualMachineName')]",
                                          "type": "Microsoft.Compute/virtualMachines",
                                          "apiVersion": "2021-07-01",
                                          "location": "[parameters('location')]",
                                          "dependsOn": [
                                              "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
                                          ],
                                          "properties": {
                                              "hardwareProfile": {
                                                  "vmSize": "[parameters('virtualMachineSize')]"
                                              },
                                              "storageProfile": {
                                                  "osDisk": {
                                                      "createOption": "fromImage",
                                                      "managedDisk": {
                                                          "storageAccountType": "[parameters('osDiskType')]"
                                                      },
                                                      "deleteOption": "[parameters('osDiskDeleteOption')]"
                                                  },
                                                  "imageReference": {
                                                      "publisher": "MicrosoftWindowsServer",
                                                      "offer": "WindowsServer",
                                                      "sku": "2019-datacenter-gensecond",
                                                      "version": "latest"
                                                  }
                                              },
                                              "networkProfile": {
                                                  "networkInterfaces": [
                                                      {
                                                          "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]",
                                                          "properties": {
                                                              "deleteOption": "[parameters('nicDeleteOption')]"
                                                          }
                                                      }
                                                  ]
                                              },
                                              "osProfile": {
                                                  "computerName": "[parameters('virtualMachineComputerName')]",
                                                  "adminUsername": "[parameters('adminUsername')]",
                                                  "adminPassword": "[parameters('adminPassword')]",
                                                  "windowsConfiguration": {
                                                      "enableAutomaticUpdates": true,
                                                      "provisionVmAgent": true,
                                                      "patchSettings": {
                                                          "enableHotpatching": "[parameters('enableHotpatching')]",
                                                          "patchMode": "[parameters('patchMode')]"
                                                      }
                                                  }
                                              },
                                              "licenseType": "Windows_Server"
                                          },
                                          "tags": "[parameters('tags')]"
                                      }
                                  ],
                                  "outputs": {
                                      "adminUsername": {
                                          "type": "string",
                                          "value": "[parameters('adminUsername')]"
                                      }
                                  }
                              }
    

    deploy.parameter.json

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "location": {
                "value": "westus2"
            },
            "networkInterfaceName": {
                "value": "ajayxxxxx_z1"
            },
            "enableAcceleratedNetworking": {
                "value": true
            },
            "networkSecurityGroupName": {
                "value": "ajaytxxxx-nsg"
            },
            "networkSecurityGroupRules": {
                "value": []
            },
            "subnetName": {
                "value": "default"
            },
            "virtualNetworkName": {
                "value": "ajxxxx"
            },
            "addressPrefixes": {
                "value": [
                    "10.0.0.0/16",
                    "172.50.28.0/22"
                ]
            },
            "subnets": {
                "value": [
                    {
                        "name": "default",
                        "properties": {
                            "addressPrefix": "10.0.0.0/24"
                        }
                    }
                ]
            },
            "publicIpAddressName": {
                "value": "ajayxxxx-ip"
            },
            "publicIpAddressType": {
                "value": "Static"
            },
            "publicIpAddressSku": {
                "value": "Standard"
            },
            "pipDeleteOption": {
                "value": "Detach"
            },
            "virtualMachineName": {
                "value": "ajayxxxx"
            },
        
            "virtualMachineComputerName": {
                "value": "ajayxxx"
            },
            "virtualMachineRG": {
                "value": "v-xxxxxxxx"
            },
            "osDiskType": {
                "value": "Premium_LRS"
            },
            "osDiskDeleteOption": {
                "value": "Delete"
            },
            "virtualMachineSize": {
                "value": "Standard_D2s_v3"
            },
            "nicDeleteOption": {
                "value": "Detach"
            },
            "adminUsername": {
                "value": "Admini"
            },
            "adminPassword": {
                "value": "passyours"
            },
            "patchMode": {
                "value": "AutomaticByOS"
            },
            "enableHotpatching": {
                "value": false
            },
            "tags":{
                "value":{
                    "ajtest": "testdev",
                    "owning": "ajtest"
                }
            }
        
        }
    }
    

    We have used below command in our environment and it works as well ,also you can use New-AzResourceGroupDeployment as mentioned in doc. with Connect-AzAccount in PowerShell.

    deployment command:-

    az deployment group create -n TestDeployment -g rgName --template-file "C:\Users\source\repos\virtualmachinearm\vmvn.json" --parameters "C:\User\source\repos\azuredeploy.parameters.json"
    

    OUTPUT SCREENSHOT FOR REFERENCE:-

    enter image description here enter image description here enter image description here

    enter image description here

    Deployed using New-AzResourceGroupDeployment as well :-

    enter image description here

    PowerShell version:-

    enter image description here

    NOTE:- When using az deployment group create need to use cli(az login) and when using New-new-Azresourcegroupdeployment(Connect-AzAccount)

    For more information please refer this MICROSOFT DOCUMENTATION| Create Virtual Machine using Template.