由于 api 版本无效,无法创建 Azure TemplateSpec

问题描述 投票:0回答:2

我正在尝试使用 Bicep 和 Az-PS 中的 New-AzTemplateSpec 命令创建 TemplateSpec。但是,我遇到了错误,需要一些帮助来解决它:

{
  "error": {
    "code": "InvalidSchema",
    "message": "The template is invalid. Error: 'The template resource 'subscriptionDeployment' at line '168' and column '18' is invalid. The api-version '2018-11-01' used to deploy the template does not support 'Scope' property. Please use api-version '2019-05-01' or later to deploy the template. Please see https://aka.ms/arm-syntax-resources for usage details.'"
  }
}

这是执行后编译的 ARM 模板的相应片段:

{
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "subscriptionDeployment",
      "scope": "/",
      "location": "[deployment().location]",

从此二头肌片段创建:

resource subscriptionRes 'Microsoft.Subscription/aliases@2021-10-01' = {
  name: subscriptionName
  scope: tenant()
  properties: {
    displayName: subscriptionName
    billingScope: '/billingAccounts/${billingAccountName}/billingProfiles/${billingProfileName}/invoiceSections/${invoiceSectionName}'
    additionalProperties: {
      managementGroupId: managementGroupId
    }
  }
}

这就是我的 PS 命令的样子:

New-AzTemplateSpec -Name name -Version "0.0.1" -ResourceGroupName rgname -Location westeurope -TemplateFile "<path_to_bicep_file>"

我发现了类似的帖子,提到 Cmdlet 需要更新,这是我不太确定的部分。我认为负责创建 TemplateSpec 的 Az.Resources PowerShell 模块是最新版本:

Get-InstalledModule -Name Az.Resources

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
6.9.0                Az.Resources                        PSGallery            Microsoft Azure PowerShell - Azure Resource Manager and Active Directory cmdlets in Windows PowerShell and …

另外,我正在使用最新的 PowerShell 7 版本:

$PSVersionTable. PSVersion

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      3      6

非常感谢任何建议!

编辑: 我从列出已安装的 Microsoft.Resources 模块的 api 版本中获得的输出如下:

(Get-AzResourceProvider -ProviderNamespace Microsoft.Resources).ResourceTypes | Where-Object ResourceTypeName -eq 'templateSpecs'

ResourceTypeName : templateSpecs
Locations        : {East Asia, Southeast Asia, Australia East, Australia Central…}
ApiVersions      : {2022-02-01, 2021-05-01, 2021-03-01-preview, 2019-06-01-preview}

编辑2: 这是我将部署导入主二头肌文件的方法:

// Subscription
module sub 'artifacts/subscription.bicep' = {
  scope: tenant()
  name: 'subscriptionDeployment'
  params: {
    managementGroupId: managementGroupId
    subscriptionName: subscriptionName
    billingAccountName: billingAccountName
    billingProfileName: billingProfileName
    invoiceSectionName: invoiceSectionName
    budgetName: '${subShortName}-budget'
    amount: amount
    category: category
    timeGrain: timeGrain
    startDate: startDate
    endDate: endDate
  }
}
azure azure-resource-manager azure-powershell azure-rm-template azure-bicep
2个回答
0
投票

该错误表明模板的 Api 版本

'2018-11-01'
不支持
'Scope'
属性。要部署模板,您必须使用 Api 版本
'2019-05-01'
或更高版本。

我看到您正在运行 Api 版本

"apiVersion":"2022-09-01"
。尝试将其修改为
"apiVersion":"2019-05-01"
并部署以检查兼容性。

您提到您正在使用最新版本的

Az.Resources
PowerShell 模块,但该版本可能不支持必需的 Api 版本。

注:

  1. 如果需要,请使用以下命令更新

    Az.resources
    模块。

    Update-Module -Name Az.Resources -Force

  2. 尝试检索提供程序及其在您的环境中可用并注册的 Api 版本,如图所示。

Get-AzResourceProvider -Listavailable
(Get-AzResourceProvider -ProviderNamespace Microsoft.Resources).ResourceTypes | Where-Object ResourceTypeName -eq 'templateSpecs'

通过参考用于存储的MSDoc模板规范部署模板,我通过将-version参数更改为

-Version "2.0"
在我的环境中尝试了类似的部署,并且它按预期工作。

New-AzTemplateSpec -Name xxxx -Version "2.0" -ResourceGroupName <resourcegroup> -Location westus2 -TemplateFile "stoa.bicep"     

enter image description here

如果问题仍然存在,请清除缓存并重新部署模板规范部署。

更新:

targetScope = 'tenant'
var SubscriptionName = 'Free Trial'
resource Subscription  'Microsoft.Subscription/aliases@2020-09-01' = {
scope: tenant()
name:guid(SubscriptionName,  tenant().tenantId)
properties: {
displayName: SubscriptionName
billingScope: '/providers/Microsoft.Billing/billingAccounts/xxxxx'
workload: 'Production'
   }
}

我尝试了上面的二头肌代码,使用 api-version

2020-09-01
订阅别名,部署成功。

enter image description here

enter image description here


0
投票

好吧,我现在能够找出问题所在了。我给模块的范围是错误的:

module sub 'artifacts/subscription.bicep' = { 
scope: tenant()

我需要将模块的范围设置为“managementGroup”,并将目标范围设置为我导入的订阅二头肌文件中的“managementGroup”(我在本文中发现了这一点)。资源定义本身的scope属性仍然需要是tenant()。

非常感谢 Jahnavi 帮助我完成整个过程。

© www.soinside.com 2019 - 2024. All rights reserved.