43

我正在尝试使用AzureResourceManager PowerShell 模块来创建和配置网站。我从 Visual Studio 生成的模板文件开始,当我通过New-AzureResourceGroup -TemplateFile website.json.

所以现在我正在尝试调整模板文件来配置站点。我正在尝试设置 php 和 .NET Framework 版本。根据架构,这些属性是通过资源数组中的配置对象设置的。

这是我的 json 模板的网站部分。“资源”部分是我添加的:

    {
        "apiVersion": "2014-06-01",
        "name": "[parameters('siteName')]",
        "type": "Microsoft.Web/sites",
        "location": "[parameters('siteLocation')]",
        "tags": {
            "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource"
        },
        "dependsOn": [
            "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
        ],
        "properties": {
            "name": "[parameters('siteName')]",
            "serverFarm": "[parameters('hostingPlanName')]"
        },
        "resources": [
            {
                "apiVersion": "2014-04-01",
                "type": "Microsoft.Web/sites/config",
                "name": "config",
                "properties": {
                    "name": "config",
                    "phpVersion": "",
                    "netFrameworkVersion": "V4.5"
                }
            }
        ]
    },

当我将此模板传递给Test-AzureResourceGroupTemplate我时收到此错误:

Code    : InvalidTemplate
Message : Deployment template validation failed: 'The template resource 'config' for type 'Microsoft.Web/sites/config' 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'.

我找不到任何关于此的文档。有谁知道这个错误是什么意思,或者我做错了什么?

4

5 回答 5

66

永远不会失败,只要我写出问题,我就会找到答案。

该错误意味着因为这是一个嵌套资源(配置对象嵌套在站点对象内),所以名称需要反映这一点。因此,而不是config名称应该是mysite/config. 我还需要添加该dependsOn部分。这是成功验证的模板:

"resources": [
    {
        "apiVersion": "2014-04-01",
        "type": "Microsoft.Web/sites/config",
        "name": "[concat(parameters('siteName'), '/config')]",
        "dependsOn": [
            "[concat('Microsoft.Web/sites/', parameters('siteName'))]"
        ],
        "properties": {
            "phpVersion": "",
            "netFrameworkVersion": "V4.5"
        }
    }
]
于 2014-11-05T21:13:10.280 回答
18

我遇到了同样的问题,其他答案都没有对我有用,结果实际上这比其他答案显示的要多一些。首先,对于根级资源,文档指定它必须:

...名称中的段比资源类型中的段少

换句话说,如果您正在创建:

"type": "Microsoft.Web/sites"

那么由于名称必须比类型少一个段,所以在这个例子中我们只能使用一个段作为名称,即一个:

"name": "MySite"

对于嵌套资源,规则是:

类型和名称具有相同数量的段

但是,这假设您正在缩短嵌套资源的类型,例如创建“Microsoft.Web/sites/config”类型作为“Microsoft.Web/sites”类型的父级中的嵌套资源,并为嵌套资源指定:

"type": "config"

所以在这里你也可以只指定一个段名,例如:

"name": "MyConfig"

因此,将所有这些放在一起,您将拥有:

{
  "type": "Microsoft.Web/sites",
  "name": "MySite",
  "various other properties": ...,
  "resources": [
    {
      "type": "config",
      "name": "MyConfig"
      "various other properties": ...
    }
  ]
}

另一方面,如果您在嵌套资源中指定完整的类型名称(如已接受的答案所示),则需要采用根命名约定,即名称中的段比类型少一个!转换上述内容您将拥有:

{
  "type": "Microsoft.Web/sites",
  "name": "MySite",
  "various other properties": ...,
  "resources": [
    {
      "type": "Microsoft.Web/sites/config",
      "name": "MySite/MyConfig"
      "various other properties": ...
    }
  ]
}
于 2019-03-20T21:57:01.500 回答
7

对于非英语母语,“不正确的段长度”错误消息很难理解,因此有简单的英语/json 解释:例如,您有一个资源类型的 Microsoft.Network/trafficManagerProfiles资源,由于某种原因,您需要将嵌套资源定义 Microsoft.Network/trafficManagerProfiles/ExternalEndpoints为单独的资源。

嵌套资源必须有名称parent_resource_name/nested_res_name

正确的(简化的)架构是:

{
  "type": "Microsoft.Network/trafficManagerProfiles",
  "name": "[variables('trafManagerProfileName')]",
   ...
},
{
  "type": "Microsoft.Network/trafficManagerProfiles/ExternalEndpoints",
  "name": "[concat(variables('trafManagerProfileName'), '/Endpoint', copyIndex())]",
  "dependsOn": [
    "[concat('Microsoft.Network/trafficManagerProfiles/', variables('trafManagerProfileName'))]",
    "[parameters('app_name')]" # where the endpoint should look at
  ],
   ...
}

ps 如果您需要根据第三个资源的数量动态生成嵌套资源,您可能也会对这个问题感兴趣:如何在 ARM 模板中动态生成流量管理器端点?

于 2018-04-11T10:29:54.160 回答
1

Vnet 对等互连在我身上发生了这样的事情。我使用了微软文档中的这个模板:

{
     "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
     "contentVersion": "1.0.0.0",
     "parameters": {
     },
     "variables": {
     },
 "resources": [
         {
         "apiVersion": "2016-06-01",
         "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
         "name": "myVnetA/myVnetAToMyVnetB",
         "location": "[resourceGroup().location]",
         "properties": {
         "allowVirtualNetworkAccess": true,
         "allowForwardedTraffic": false,
         "allowGatewayTransit": false,
         "useRemoteGateways": false,
             "remoteVirtualNetwork": {
             "id": "/subscriptions/<subscription ID>/resourceGroups/PeeringTest/providers/Microsoft.Network/virtualNetworks/myVnetB"
             }
         }
         }
     ]
}

但是后来我将其修改name为例如,vneta-tovnetb它给了我错误。

如果我使用它vneta/vnetbname它会验证。

于 2020-11-12T19:27:57.357 回答
0

你可以在这里找到答案。对于子资源,类型和名称具有相同数量的段。这个段数是有意义的,因为孩子的全名和类型包括父名称和类型。因此,全名仍然比全名少一个段。

https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/error-invalid-template

于 2020-05-04T12:51:09.440 回答