通过 Azure CLI 在管道中创建资源组时出现“InvalidOperationString”错误二头肌

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

我收到以下错误。

{"status":"Failed","error":{"code":"DeploymentFailed","target":"/subscriptions/{subscription_id}/providers/Microsoft.Resources/deployments/assign_role_to_subscription","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-deployment-operations for usage details.","details":
[{"code":"InvalidOperationString",
"message":"Invalid operation string {subscription_name} provided in request"}]}}

我正在二头肌文件下运行:

import * as common from 'exports.bicep'
targetScope='subscription'

//  Parameters
param resourceGroupName string = 'rg-ascend-us'
param resourceGroupLocation string = 'eastus2'
param environmentName string = 'Development'

var peregrineGroupPrincipalId = '{group_principal_id}'
var readerRoleDefinitionId = 'acdd72a7-3385-48ef-bd42-f606fba81ae7'
var subscriptionName = 'Cosmos Dev'

//  Get existing subscription
@description('')
resource subscriptionResource 'Microsoft.Subscription/subscriptionOperations@2017-11-01-preview' existing = {
  scope: tenant()
  name: subscriptionName
}

//  Get the reader role.
@description('This is the built-in Reader role. See acdd72a7-3385-48ef-bd42-f606fba81ae7')
resource readerRoleDefinition 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = {
  scope: subscription()
  name: readerRoleDefinitionId
}

//  Assign group 'sg-apac-peregrine' to the subscription and set the role.
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
  scope: subscriptionResource
  name: guid(subscription().id, peregrineGroupPrincipalId, readerRoleDefinition.id)
  properties:{
    roleDefinitionId: readerRoleDefinition.id
    principalId: peregrineGroupPrincipalId
    principalType: 'Group'
  }
}

resource resourceGroup 'Microsoft.Resources/resourceGroups@2024-03-01' = {
  name: resourceGroupName
  location: resourceGroupLocation
  tags: common.getTags(environmentName)
}

二头肌文件是使用 azure CLI 命令通过 Azure Devops Pipeline 作业执行的。

- task: AzureCLI@2
  inputs:
    azureSubscription: 'Azure Cosmos Dev Subscription Connection'
    scriptType: bash
    scriptLocation: inlineScript
    useGlobalConfig: false
    inlineScript: |
      az deployment sub create --location eastus2 --template-file $(System.DefaultWorkingDirectory)/.iac/bicep/assign_role_to_subscription.bicep

CLI 命令是

az deployment sub create --location eastus2 --template-file {path_to_bicep_file}

服务连接与我尝试部署的订阅关联。该错误消息对于准确显示发生的情况没有多大帮助。任何见解将不胜感激。

azure azure-pipelines azure-bicep
1个回答
0
投票

根据您分享的二头肌样本,我尝试进行一些更改,并且可以成功运行。

这是示例:

出口.bicep

@export()
func getTags(environmentName string) object => {
  Product: 'product_name'
  Team: 'team_name'
  ServiceName: 'adapter'
  Environment: environmentName
}

主要二头肌:

import * as common from 'exports.bicep'
targetScope='subscription'

//  Parameters
param resourceGroupName string = 'rg-ascend-us'
param resourceGroupLocation string = 'eastus2'
param environmentName string = 'Development'

var peregrineGroupPrincipalId = '{group_principal_id}'
var readerRoleDefinitionId = 'acdd72a7-3385-48ef-bd42-f606fba81ae7'
var subscriptionName = 'Cosmos Dev'

//  Get existing subscription
@description('')
resource subscriptionResource 'Microsoft.Subscription/subscriptionOperations@2017-11-01-preview' existing = {
  scope: tenant()
  name: subscriptionName
}

//  Get the reader role.
@description('This is the built-in Reader role. See acdd72a7-3385-48ef-bd42-f606fba81ae7')
resource readerRoleDefinition 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = {
  scope: subscription()
  name: readerRoleDefinitionId
}

//  Assign group 'sg-apac-peregrine' to the subscription and set the role.
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
  scope: subscriptionResource
  name: guid(subscription().id, peregrineGroupPrincipalId, readerRoleDefinition.id)
  properties:{
    roleDefinitionId: readerRoleDefinition.id
    principalId: peregrineGroupPrincipalId
    principalType: 'Group'
  }
}

resource resourceGroup 'Microsoft.Resources/resourceGroups@2024-03-01' = {
  name: resourceGroupName
  location: resourceGroupLocation
  tags: common.getTags(environmentName)
}

结果:

enter image description here

如果您仍然遇到同样的问题,您可以导航至 Azure 门户 -> Azure 订阅 -> 部署 查找详细日志。

更多详细信息,您可以参考此文档:订阅部署

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