当前位置: 首页 > 知识库问答 >
问题:

模板中未定义资源

巩阳秋
2023-03-14

以下模板部署:

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有关用法的详细信息。"

我已经在其他模板中创建了虚拟网络,并在这里使用相同的资源组。但是,我还是得到了以上的错误。请指教!

共有3个答案

罗伟兆
2023-03-14

我在搜索相同的错误代码时遇到了这个问题。但我有一个不同的问题:我引用了模板中另一个资源的子资源。我想这些都是在当前模板之外考虑的。

例如

{
    "$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"
            ],
        }
    ]
}
璩涛
2023-03-14

对于搜索“模板中未定义资源”而在此处结束的任何其他人,此错误消息的另一个可能原因是表单引用:

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')
陶成济
2023-03-14

删除代码中的Vnet,只有当该资源是模板的一部分时才需要它,而不是当它已经部署时才需要它。

 类似资料:
  • Azure新手,创建了一个非常基本的ARM模板。有人能告诉我为什么我经常收到以下错误:“部署模板验证失败:'模板中未定义资源'Microsoft.Network/virtualNetworks/vNet'。请参阅https://aka.ms/arm-template有关用法的详细信息。(代码:InvalidTemplate)” 我试图删除“依赖”条目,但没有成功。 谢谢 { }

  • 尝试构建ARM模板以部署多个VM。但是,模板验证失败,并显示以下错误消息 部署模板验证失败:“资源”为Microsoft。模板中未定义“网络/网络接口/sqlnodeNic”。请看https://aka.ms/arm-template有关用法的详细信息。

  • 在 cmf中模板就是一个 html 文件,可分为前台模板文件和后台模板文件; 前台模板位于 themes 目录下,后台模板位于 admin/themes 目录下,前后台都是多主题机制的,可以分开设置不同的主题; 前台默认模板是 simplebootx,以后也可能会换,我们先以这个为例; 这是前台模板的结构;应用之间彼此分开,Portal目录下就对应的是application/Portal应用的模板

  • 问题内容: 使用我正在尝试在模板中使用自己的功能之一。不幸的是,我无法使用go模板的功能映射功能。我得到的只是以下错误: 简化的测试用例如下所示(): 我有以下简单模板(): 这是1.1.1。 问题答案: IIRC,必须在解析模板之前定义模板功能图。下面的代码似乎有效。

  • 问题内容: Helm允许在Kubernetes的资源文件中使用Go模板。 通常使用一个名为的文件通过以下语法定义Go模板助手: 然后可以在资源文件中使用它,如下所示: 问题 如何在其他帮助程序定义中使用定义的帮助程序? 例如,如果我有一个用于应用程序名称的助手,并想在定义入口主机名的助手的定义中使用该助手,该怎么办? 我尝试了几种其他方式来调用其他定义中的帮助器。鉴于此基本辅助功能: 我尝试了以下

  • 正如我们已经描述过的,模板可以使用在数据模型中定义的变量。 在数据模型之外,模板本身也可以定义变量来使用。 这些临时变量可以使用FTL指令来创建和替换。请注意每一次的 模板执行 工作都维护它自己的私有变量, 同时来渲染页面。变量的初始值是空,当模板执行工作结束这些变量便被销毁了。 可以访问一个在模板里定义的变量,就像是访问数据模型根root上的变量一样。 这个变量比定义在数据模型中的同名参数有更高