You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建ARM Pipeline遍历订阅内所有SQL托管实例部署指标告警

自动遍历Azure订阅内所有SQL托管实例部署指标警报的Pipeline解决方案

需求说明

  • 为同一Azure订阅内的所有SQL Managed Instance部署指标警报
  • 每个实例对应独立资源组,每周会新增大量实例,无需手动指定资源名称,需自动遍历类型为Microsoft.Sql/managedInstances的资源

修改后的完整Pipeline YAML

pool:
  name: Azure Pipelines

jobs:
- job: FetchManagedInstances
  displayName: '获取订阅内所有SQL托管实例'
  pool:
    vmImage: 'windows-latest'
  outputs:
    managedInstances: $[steps.get_instances.outputs.managedInstances]
  steps:
  - task: AzureCLI@2
    displayName: '查询SQL托管实例列表'
    name: get_instances
    inputs:
      azureSubscription: 'ADO_Pipeline_AI'
      scriptType: 'pscore'
      scriptLocation: 'inlineScript'
      inlineScript: |
        # 查询订阅内所有SQL托管实例,提取资源组和实例名称
        $instances = az resource list --resource-type "Microsoft.Sql/managedInstances" --query "[].{resourceGroup: resourceGroup, name: name}" | ConvertFrom-Json
        # 转换为Pipeline可识别的JSON格式并输出
        $jsonOutput = $instances | ConvertTo-Json -Compress
        Write-Host "##vso[task.setvariable variable=managedInstances;isOutput=true]$jsonOutput"

- job: DeployMetricAlerts
  displayName: '批量部署指标警报'
  pool:
    vmImage: 'windows-latest'
  dependsOn: FetchManagedInstances
  variables:
    managedInstances: $[dependencies.FetchManagedInstances.outputs['get_instances.managedInstances']]
  steps:
  - ${{ each instance in convertToJson(variables.managedInstances) }}:
    - task: AzureResourceManagerTemplateDeployment@3
      displayName: '为${{ instance.name }}部署CPU利用率指标警报'
      inputs:
            deploymentScope: Resource Group
            azureResourceManagerConnection: 'ADO_Pipeline_AI'
            subscriptionId: '<替换为你的Azure订阅ID>'
            resourceGroupName: ${{ instance.resourceGroup }}
            location: "Global"
            templateLocation: Linked artifact
            csmFile: '**/Templates/metric_alert_static.json'
            csmParametersFile: '**/Parameter_Files/CPU_usage_SQL_managed_ins.json'
            deploymentMode: Incremental
    - task: AzureResourceManagerTemplateDeployment@3
      displayName: '为${{ instance.name }}部署IO请求数指标警报'
      inputs:
            deploymentScope: Resource Group
            azureResourceManagerConnection: 'ADO_Pipeline_AI'
            subscriptionId: '<替换为你的Azure订阅ID>'
            resourceGroupName: ${{ instance.resourceGroup }}
            location: "Global"
            templateLocation: Linked artifact
            csmFile: '**/Templates/metric_alert_dynamic.json'
            csmParametersFile: '**/Parameter_Files/IO_req_count.json'
            deploymentMode: Incremental

核心改动说明

  • 新增实例查询作业:通过AzureCLI@2任务调用Azure CLI查询订阅内所有SQL托管实例,提取每个实例的资源组和名称,将结果输出为跨作业可用的变量
  • 动态遍历部署:依赖前置作业的实例列表,使用convertToJson和each循环,自动为每个实例部署两类指标警报,无需手动维护实例清单
  • 移除硬编码参数:删除了原有的resourceConfigurations参数,彻底避免手动指定资源名称,自动适配每周新增的SQL托管实例
  • 权限提示:确保服务连接ADO_Pipeline_AI拥有Microsoft.Sql/managedInstances的读取权限,以及资源组的模板部署权限

内容的提问来源于stack exchange,提问作者Sissi_00_20

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 02:01:04