模板中未定义该资源

44

部署以下模板:

https://gist.github.com/rnkhouse/aea0a8fd395da37b19466348b919d620

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "String"
        },
        "virtualNetworkName": {
        "type": "string",
        "metadata": {
            "description": "This is the name of the Virtual Network"
        }
        },
        "networkInterfaceName": {
        "type": "string",
        "metadata": {
            "description": "This is the prefix name of the Network interfaces"
        }
        },
        "loadBalancerName": {
        "type": "string",
        "metadata": {
            "description": "This is the name of the load balancer"
        }
        },
        "adminUsername": {
        "type": "string",
        "metadata": {
            "description": "Admin username"
        }
        },
        "adminPublicKey": {
        "type": "string",
        "metadata": {
            "description": "SSH Public Key"
        }
        },
        "imagePublisher": {
        "type": "string",
        "defaultValue": "Canonical",
        "metadata": {
            "description": "Image Publisher"
        }
        },
        "vmNamePrefix": {
        "type": "string",
        "metadata": {
            "description": "Prefix to use for VM names"
        }
        },
        "imageOffer": {
        "type": "string",
        "defaultValue": "UbuntuServer",
        "metadata": {
            "description": "Image Offer"
        }
        },
        "imageSKU": {
        "type": "string",
        "defaultValue": "14.04.5-LTS",
        "metadata": {
            "description": "Image SKU"
        }
        },
        "vmStorageAccountContainerName": {
        "type": "string",
        "defaultValue": "vhds",
        "metadata": {
            "description": "This is the storage account container name"
        }
        },
        "storageAccountName": {
        "type": "string",
        "metadata": {
            "description": "Storage account name"
        }
        },
        "vmSize": {
        "type": "string",
        "defaultValue": "Standard_D1",
        "metadata": {
            "description": "This is the allowed list of VM sizes"
        }
        },
        "subnetName": {
            "defaultValue": "subnet-2",
            "type": "String"
        }
    },
    "variables": {
        "availabilitySetName": "[concat(parameters('subnetName'),'-AVSET')]",
        "addressPrefix": "1.0.0.0/16",
        "subnetPrefix": "1.0.2.0/24",
        "storageAccountType": "Standard_LRS",
        "vnetID": "[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks',parameters('virtualNetworkName'))]",
        "subnetRef": "[concat(variables('vnetID'),'/subnets/',parameters ('subnetName'))]",
        "numberOfInstances": 2,
        "lbID": "[resourceId('Microsoft.Network/loadBalancers',parameters('loadBalancerName'))]"
    },
    "resources": [
        {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Storage/storageAccounts",
        "name": "[parameters('storageAccountName')]",
        "location": "[parameters('location')]",
        "properties": {
            "accountType": "[variables('storageAccountType')]"
        }
        },
        {
        "apiVersion": "2016-04-30-preview",
        "type": "Microsoft.Compute/availabilitySets",
        "name": "[variables('availabilitySetName')]",
        "location": "[parameters('location')]",
        "properties": {
            "platformFaultDomainCount": "2",
            "platformUpdateDomainCount": "2",
            "managed": "true"
        }
        },
        {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Network/virtualNetworks/subnets",
        "name": "[concat(parameters('virtualNetworkName'), '/', parameters('subnetName'))]",
        "location": "[parameters('location')]",
        "properties": {
            "addressPrefix": "[variables('subnetPrefix')]"
        }
        },
        {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Network/networkInterfaces",
        "name": "[concat(parameters('networkInterfaceName'), copyindex())]",
        "location": "[parameters('location')]",
        "copy": {
            "name": "nicLoop",
            "count": "[variables('numberOfInstances')]"
        },
        "dependsOn": [
            "[concat('Microsoft.Network/loadBalancers/', parameters('loadBalancerName'))]"
        ],
        "properties": {
            "ipConfigurations": [
            {
                "name": "ipconfig1",
                "properties": {
                "privateIPAllocationMethod": "Dynamic",
                "subnet": {
                    "id": "[variables('subnetRef')]"
                },
                "loadBalancerBackendAddressPools": [
                    {
                    "id": "[concat(variables('lbID'), '/backendAddressPools/BackendPool1')]"
                    }
                ]
                }
            }
            ]
        }
        },
        {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Network/loadBalancers",
        "name": "[parameters('loadBalancerName')]",
        "location": "[parameters('location')]",
        "dependsOn": [],
        "properties": {
            "frontendIPConfigurations": [
            {
                "properties": {
                "subnet": {
                    "id": "[variables('subnetRef')]"
                },
                "privateIPAddress": "1.0.2.50",
                "privateIPAllocationMethod": "Static"
                },
                "name": "LoadBalancerFrontend"
            }
            ],
            "backendAddressPools": [
            {
                "name": "BackendPool1"
            }
            ],
            "loadBalancingRules": [
            {
                "properties": {
                "frontendIPConfiguration": {
                    "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancerName')), '/frontendIpConfigurations/LoadBalancerFrontend')]"
                },
                "backendAddressPool": {
                    "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancerName')), '/backendAddressPools/BackendPool1')]"
                },
                "probe": {
                    "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancerName')), '/probes/lbprobe')]"
                },
                "protocol": "Tcp",
                "frontendPort": 80,
                "backendPort": 80,
                "idleTimeoutInMinutes": 15
                },
                "Name": "lbrule"
            }
            ],
            "probes": [
            {
                "properties": {
                "protocol": "Tcp",
                "port": 80,
                "intervalInSeconds": 15,
                "numberOfProbes": 2
                },
                "name": "lbprobe"
            }
            ]
        }
        },
        {
        "apiVersion": "2016-04-30-preview",
        "type": "Microsoft.Compute/virtualMachines",
        "name": "[concat(parameters('vmNamePrefix'), copyindex())]",
        "copy": {
            "name": "virtualMachineLoop",
            "count": "[variables('numberOfInstances')]"
        },
        "location": "[parameters('location')]",
        "dependsOn": [
            "[concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]",
            "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'), copyindex())]",
            "[concat('Microsoft.Compute/availabilitySets/', variables('availabilitySetName'))]"
        ],
        "properties": {
            "availabilitySet": {
            "id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName'))]"
            },
            "hardwareProfile": {
            "vmSize": "[parameters('vmSize')]"
            },
            "osProfile": {
            "computerName": "[concat(parameters('vmNamePrefix'), copyIndex())]",
            "adminUsername": "[parameters('adminUsername')]",
            "linuxConfiguration": {
                "disablePasswordAuthentication": "true",
                "ssh": {
                    "publicKeys": [
                        {
                            "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
                            "keyData": "[parameters('adminPublicKey')]"
                        }
                    ]
                }
            }
            },
            "storageProfile": {
            "imageReference": {
                "publisher": "[parameters('imagePublisher')]",
                "offer": "[parameters('imageOffer')]",
                "sku": "[parameters('imageSKU')]",
                "version": "latest"
            },
            "osDisk": {
                "createOption": "FromImage"
            }
            },
            "networkProfile": {
            "networkInterfaces": [
                {
                "id": "[resourceId('Microsoft.Network/networkInterfaces',concat(parameters('networkInterfaceName'),copyindex()))]"
                }
            ]
            },
            "diagnosticsProfile": {
            "bootDiagnostics": {
                "enabled": "true",
                "storageUri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net')]"
            }
            }
        }
        }
    ]
}

错误:

"部署失败,状态码为:400,消息为:“部署模板验证失败:'Microsoft.Network/virtualNetworks/mtes-dev-VNET' 资源未在模板中定义。请参阅https://aka.ms/arm-template 了解使用详情。”"

我已经在其他模板中创建了虚拟网络,并在这里使用相同的资源组。但是,我仍然遇到上述错误。请给予建议!


1
有没有可能将模板包含在问题中,而不是提供到另一个网站的链接?在某些时候,该链接可能会失效,使得这个问题变得不那么有价值;但也有一些人可能无法访问该链接,因为它可能被“阻止”(这是我的情况)。 - Richardissimo
4个回答

98

在您的代码中删除dependsOn Vnet,它只需要在该资源是模板的一部分时才需要,如果已经部署,则不需要。


1
是的,我只是在引用子网。因为我需要在新的子网中创建新的虚拟机。 - RNK
1
请参考以下翻译:请将链接中的JSON文件中第164行的内容更改为所需的Azure DB服务器名称。 - 4c74356b41
1
哦..我只是在寻找VirtualNetworks关键字而已。谢谢 :) - RNK

80

如果有其他人通过搜索'The resource is not defined in the template'而来到这里,那么出现这个错误信息的另一个可能原因是以下形式的引用:

reference('<some complete id outside this template>')
或者
listkeys('<some complete id outside this template>')

错误信息没有告诉你,但是在引用当前模板之外定义的资源时,需要包括API版本。

例如:

reference('<some complete id outside this template>', '2018-03-01')

很好的发现。文档确实涵盖了这一点,但它只在https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-resource#reference中提到:“指定资源的API版本。当资源未在同一模板中预配时,请包括此参数。” - oatsoda
5
这也是我遇到错误的原因。只想补充一点,你可以使用如下代码动态获取 Api 版本,而不是固定的版本号:providers('Microsoft.Web', 'certificates').apiVersions[0] - Céryl Wiltink
2
@CérylWiltink,这是一条危险的建议。API 版本被设计为硬编码。您的模板已针对特定的 API 版本进行编码,并且该 API 版本将有一段时间的支持。在升级期间,您可能需要更改属性结构、添加开始要求的一些属性等。不能只更改 API 版本并期望模板将继续工作。 - Palec

5
我在查找同样的错误代码时遇到了这个问题。然而我的问题是不同的:我在模板中引用了另一个资源的子资源。我猜这些被认为在当前模板之外。
例如:
{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
    "contentVersion": "1.0.0.0",
    "resources": [    
        {
            "type": "Microsoft.Network/virtualNetworks",
            "name": "vnetName",
            "location": "[resourceGroup().location]",
            "apiVersion": "2018-11-01",
            "properties": {
                ...
                }
            },
            "resources": [
                {
                    "type": "subnets",
                    "apiVersion": "2018-11-01",
                    "name": "subnetName",
                    "dependsOn": [
                        "[resourceId('Microsoft.Network/virtualNetworks', vnetName)]"
                    ],
                    "properties": {
                        ...
                    }
                }
            }
        },
        {
            "apiVersion": "2016-02-01",
            "name": "deploymentName",
            "type": "Microsoft.Resources/deployments",
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks.subnets', vnetName, subnetName)]"
            ],
        }
    ]
}

解决方法是将父资源放入部署中,并依赖于该部署。

例如:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
    "contentVersion": "1.0.0.0",
    "resources": [    
        {
            "apiVersion": "2016-02-01",
            "name": "deployment1",
            "type": "Microsoft.Resources/deployments",
            "resources": [
                {
                    "type": "Microsoft.Network/virtualNetworks",
                    "name": "vnetName",
                    "location": "[resourceGroup().location]",
                    "apiVersion": "2018-11-01",
                    "properties": {
                        ...
                    },
                    "resources": [
                        {
                            "type": "subnets",
                            "apiVersion": "2018-11-01",
                            "name": "subnetName",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/virtualNetworks', vnetName)]"
                            ],
                            "properties": {
                                ...
                            }
                        }
                    ]
                }
            ]
        },
        {
            "apiVersion": "2016-02-01",
            "name": "deployment2",
            "type": "Microsoft.Resources/deployments",
            "dependsOn": [
                "deployment1"
            ],
        }
    ]
}

太棒了。答案有效,但为什么当我指定[resourceid('Microsoft.Resources/deployments','deployment1')]时它不起作用呢? - ShresthaGanesh

1

我遇到了同样的问题,解决方案是不要使用:

"dependsOn": [
  resourceid('Microsoft.Resources/deployments','deployment1')]
]

您应该使用:

"dependsOn": [
  "deployment1"
],

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接