本文介绍了Azure ARM模板跳过嵌套条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我真的是ARM模板的新手,对于我的第一个模板,我试图创建一个问一些问题的模板,并最终创建一个新的子网和一个NSG(如果需要),然后创建一个VM及其所有组件.

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.

如果我在创建新子网"和创建新NSG"问题中均选择是",则下面粘贴的模板可以很好地工作.如果选择否",则会出现错误消息:

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:

我不知道这是什么意思,我希望回答否"只会跳过子网和NSG创建并继续,但是不会.

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'))]"
                              }
                            }
                          ]
                        }
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  ]
}

推荐答案

是的,要确切地诊断问题,我们需要您完整的模板,但基于错误,它看起来就像段长度不正确问题,即

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.,

通常,ROOT资源类型的段长度必须比其资源名称大一,即,如果您的ROOT资源类型类似于"type":"Microsoft.xxxxxxx/yyyyyyy/zzzzzzz",则其名称应类似于名称":"aaaaaaa/bbbbbbb"

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"

并且NESTED资源类型的段数必须与其资源名称相同,即,如果您的NESTED资源类型类似于"type":"Microsoft.xxxxxxx/yyyyyyy/zzzzzzz",则其名称应类似于"name" ":"aaaaaaa/bbbbbbb/ccccccc"

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"

有关更多信息,请参考08-14 05:43