Azure ARM Template Skip Nested Condition

2020-04-18 05:32发布

问题:

So I'm really new to ARM templates and for my first template I'm trying to create one that asks some questions and ultimately creates a new subnet and an NSG if required and then a VM and all it's components.

The template I've pasted below works perfectly if I select 'Yes' for both the 'Create New Subnet' and 'Create New NSG' questions. If I select 'No' I get an error saying:

The template resource 'dev-vnet/' for type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Common.Entities.TemplateGenericProperty`1[System.String]' at line '1' and column '306' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name.

I have no idea what this means and was hoping that answering 'No' would just skip the Subnet and NSG creation and continue, but it doesn't.

Could anyone help me resolve the issue please?

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "Create New Subnet": {
      "type": "string",
      "allowedValues": [
        "Yes",
        "No"
      ],
      "defaultValue": "No",
      "metadata": {
        "description": ""
      }
    },
    "New Subnet Name": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": ""
      }
    },
    "New Subnet Prefix": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": ""
      }
    },
    "Create New NSG": {
      "type": "string",
      "allowedValues": [
        "Yes",
        "No"
      ],
      "defaultValue": "No",
      "metadata": {
        "description": "Select whether the VM should be in production or not."
      }
    },
    "VM Name": {
      "type": "string",
      "metadata": {
        "description": "The name of the VM you're creating. e.g. az-manage-01"
      }
    },
    "VM Size": {
      "type": "string",
      "allowedValues": [
        "Standard_D1_v2",
        "Standard_D2s_v3"
      ],
      "metadata": {
        "description": "The compute specification required for the VM."
      }
    },
    "Update Schedule": {
      "type": "string",
      "allowedValues": [
        "Production",
        "Testing"
      ],
      "defaultValue": "Production",
      "metadata": {
        "description": "The update schedule that the VM will use."
      }
    },
    "Subnet Name": {
      "type": "string",
      "metadata": {
        "description": "The name of the subnet to deploy the VM into. e.g ActiveDirectory"
      }
    },
    "IP Address": {
      "type": "string",
      "metadata": {
        "description": "The IP address to assign to the VM. e.g. 10.0.1.5"
      }
    },
    "Local Admin Password": {
      "type": "securestring",
      "metadata": {
        "description": "Password used for local administrator account (contoso.admin) account on VM."
      }
    }
  },
  "variables": {
    "VM Resource Group": "[resourceGroup().name]",
    "Location": "[resourceGroup().location]",
    "Operating System": "2019-Datacenter",
    "Environment": "[subscription().displayName]",
    "EnvironmentLower": "[toLower(variables('Environment'))]",
    "Storage Account Name": "[concat('logs',variables('EnvironmentLower'))]",
    "Storage Resource Group": "[concat('monitoring-',variables('EnvironmentLower'),'-rg')]",
    "Image Publisher": "MicrosoftWindowsServer",
    "Image Offer": "WindowsServer",
    "Local Admin Username": "contoso.admin",
    "vNET Resource Group": "[concat('network-',variables('EnvironmentLower'),'-rg')]",
    "vNET Name": "[concat(variables('EnvironmentLower'),'-vnet')]",
    "NIC Name": "[concat(parameters('VM Name'),'-nic')]",
    "NSG Name": "[concat(parameters('New Subnet Name'),'-nsg')]",
    "Subnet Prefix": "[concat(parameters('New Subnet Prefix'),'/24')]",
    "Subnet ID": "[resourceId(variables('vNET Resource Group'), 'Microsoft.Network/virtualNetworks/subnets', variables('vNET Name'), parameters('Subnet Name'))]",
    "Recovery Vault Resource Group": "[concat('recovery-',variables('EnvironmentLower'),'-rg')]",
    "Recovery Vault Name": "[concat(variables('EnvironmentLower'),'-recovery-vault')]",
    "Recovery Vault Backup Fabric": "Azure",
    "Recovery Vault Backup Policy Name": "DefaultPolicy",
    "Recovery Vault Protection Container": "[concat('iaasvmcontainer;iaasvmcontainerv2;', resourceGroup().name, ';', parameters('VM Name'))]",
    "Recovery Vault Protected Item": "[concat('vm;iaasvmcontainerv2;', resourceGroup().name, ';', parameters('VM Name'))]"
  },
  "resources": [
    {
      "condition": "[equals(parameters('Create New NSG'), 'Yes')]",
      "apiVersion": "2015-06-15",
      "type": "Microsoft.Network/networkSecurityGroups",
      "name": "[variables('NSG Name')]",
      "location": "[variables('location')]",
      "properties": {
        "securityRules": []
      }
    },
    {
      "apiVersion": "2017-05-10",
      "name": "Nested_SubnetCreation",
      "type": "Microsoft.Resources/deployments",
      "resourceGroup": "[variables('vNET Resource Group')]",
      "dependsOn": [],
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-08-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {},
          "variables": {},
          "resources": [
            {
              "condition": "[equals(parameters('Create New Subnet'), 'Yes')]",
              "apiVersion": "2018-04-01",
              "type": "Microsoft.Network/virtualNetworks/subnets",
              "name": "[concat(variables('vNET Name'), '/', parameters('New Subnet Name'))]",
              "location": "[variables('location')]",
              "properties": {
                "addressPrefix": "[variables('Subnet Prefix')]",
                "networkSecurityGroup": {
                  "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('NSG Name'))]"
                }
              }
            },
            {
              "apiVersion": "2017-05-10",
              "name": "Nested_VMCreation",
              "type": "Microsoft.Resources/deployments",
              "resourceGroup": "[variables('VM Resource Group')]",
              "dependsOn": [],
              "properties": {
                "mode": "Incremental",
                "template": {
                  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/deploymentTemplate.json#",
                  "contentVersion": "1.0.0.0",
                  "parameters": {},
                  "variables": {},
                  "resources": [
                    {
                      "apiVersion": "2019-09-01",
                      "type": "Microsoft.Network/networkInterfaces",
                      "name": "[variables('NIC Name')]",
                      "location": "[variables('Location')]",
                      "dependsOn": [],
                      "properties": {
                        "ipConfigurations": [
                          {
                            "name": "ipconfig",
                            "properties": {
                              "privateIPAllocationMethod": "static",
                              "privateIPAddress": "[parameters('IP Address')]",
                              "subnet": {
                                "id": "[variables('Subnet ID')]"
                              }
                            }
                          }
                        ]
                      }
                    },
                    {
                      "apiVersion": "2019-03-01",
                      "type": "Microsoft.Compute/virtualMachines",
                      "name": "[parameters('VM Name')]",
                      "location": "[variables('Location')]",
                      "tags": {
                        "Update Schedule": "[parameters('Update Schedule')]",
                        "Subscription": "[subscription().displayName]"
                      },
                      "dependsOn": [
                        "[resourceId('Microsoft.Network/networkInterfaces', variables('NIC Name'))]"
                      ],
                      "properties": {
                        "licenseType": "Windows_Server",
                        "hardwareProfile": {
                          "vmSize": "[parameters('VM Size')]"
                        },
                        "osProfile": {
                          "computerName": "[parameters('VM Name')]",
                          "adminUsername": "[variables('Local Admin Username')]",
                          "adminPassword": "[parameters('Local Admin Password')]"
                        },
                        "storageProfile": {
                          "imageReference": {
                            "publisher": "[variables('Image Publisher')]",
                            "offer": "[variables('Image Offer')]",
                            "sku": "[variables('Operating System')]",
                            "version": "latest"
                          },
                          "osDisk": {
                            "name": "[concat(parameters('VM Name'),'-os')]",
                            "caching": "ReadWrite",
                            "createOption": "FromImage"
                          }
                        },
                        "networkProfile": {
                          "networkInterfaces": [
                            {
                              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('NIC Name'))]"
                            }
                          ]
                        },
                        "diagnosticsProfile": {
                          "bootDiagnostics": {
                            "enabled": true,
                            "storageUri": "[reference(resourceId(variables('Storage Resource Group'), 'Microsoft.Storage/storageAccounts', variables('Storage Account Name')), '2017-10-01').primaryEndpoints['blob']]"
                          }
                        }
                      }
                    },
                    {
                      "apiVersion": "2017-05-10",
                      "name": "Nested_AddVMToBackup",
                      "type": "Microsoft.Resources/deployments",
                      "resourceGroup": "[variables('Recovery Vault Resource Group')]",
                      "dependsOn": [
                        "[concat('Microsoft.Compute/virtualMachines/', parameters('VM Name'))]"
                      ],
                      "properties": {
                        "mode": "Incremental",
                        "template": {
                          "$schema": "https://schema.management.azure.com/schemas/2019-08-01/deploymentTemplate.json#",
                          "contentVersion": "1.0.0.0",
                          "parameters": {},
                          "variables": {},
                          "resources": [
                            {
                              "name": "[concat(variables('Recovery Vault Name'), '/', variables('Recovery Vault Backup Fabric'), '/', variables('Recovery Vault Protection Container'), '/', variables('Recovery Vault Protected Item'))]",
                              "apiVersion": "2016-06-01",
                              "location": "[resourceGroup().location]",
                              "type": "Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems",
                              "properties": {
                                "protectedItemType": "Microsoft.Compute/virtualMachines",
                                "policyId": "[resourceId('Microsoft.RecoveryServices/vaults/backupPolicies', variables('Recovery Vault Name'), variables('Recovery Vault Backup Policy Name'))]",
                                "sourceResourceId": "[resourceId(subscription().subscriptionId,variables('VM Resource Group'),'Microsoft.Compute/virtualMachines',parameters('VM Name'))]"
                              }
                            }
                          ]
                        }
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  ]
}

回答1:

Yes, to diagnose the issue exactly we would need your complete template but based on the error it just looks like incorrect segment length issue i.e.,

in general a ROOT resource type must have segment length one greater than its resource name i.e., if your ROOT resource type is something like "type": "Microsoft.xxxxxxx/yyyyyyy/zzzzzzz" then it's name should be something like "name": "aaaaaaa/bbbbbbb"

and a NESTED resource type must have identical number of segments as its resource name i.e., if your NESTED resource type is something like "type": "Microsoft.xxxxxxx/yyyyyyy/zzzzzzz" then it's name should be something like "name": "aaaaaaa/bbbbbbb/ccccccc"

For more information, please refer this document.