Azure网站资源模板错误

问题描述 投票:20回答:3

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

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

这是我的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'.

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

powershell azure azure-web-sites azure-resource-manager
3个回答
39
投票

永远不会失败,一旦我写出问题,我就会找出答案。

该错误意味着因为这是一个嵌套资源(配置对象嵌套在站点对象中),名称需要反映这一点。所以,而不是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"
        }
    }
]

4
投票

对于非英语原生,“错误的段长度”错误消息很难理解,因此在简单的英语/ 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
  ],
   ...
}

附:如果您需要根据第三个资源的计数动态生成嵌套资源,您可能也对这个问题感兴趣:How do I dynamically generate Traffic Manager endpoints in an ARM template?


2
投票

我遇到了同样的问题而且其他答案都没有对我有用,事实证明,这比其他答案显示的要多一些。首先,对于根级资源,the docs specify必须:

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

换句话说,如果你正在创建一个:

"type": "Microsoft.Web/sites"

然后,由于名称必须比类型少一个段,我们在此示例中只能使用单个段作为名称,即:

"name": "MySite"

对于嵌套资源,规则是:

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

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

"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": ...
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.