使用维护配置和 Bicep 来控制更新

适用于:✔️ Linux VM ✔️ Windows VM ✔️ 灵活规模集 ✔️ 统一规模集

可以使用维护配置功能来控制何时将平台更新应用到各种 Azure 资源。 本文介绍了使用此功能所需的 Bicep 语言。 若要详细了解使用维护配置功能的好处、该功能的限制以及其他管理选项,请参阅使用维护配置管理平台更新

重要

特定的范围支持特定的机器类型和计划。 请务必为 VM 选择正确的范围。

先决条件

继续操作之前,请访问 Bicep 模块,详细了解如何才能将 bicep 部署组织成模块

  • 在继续使用 Bicep 模板之前,应该了解资源部署的范围。 有关详细信息,请参阅 Bicep 的范围函数
  • 了解如何部署资源以及在何处部署资源很重要。
  • 必须使用 Bicep 模块来执行部署。 部署不同范围的资源需要使用模块。

提示

由于模块在不同范围内执行,因此请始终提供正确的依赖项集。 例如,使用动态范围时,维护配置中的配置分配范围应为 Subscription,而使用静态分配(未配置动态范围)时该范围应为 ResourceId

Bicep 资源定义

在继续使用工作示例之前,请参阅以下指南,了解 Bicep 模板的资源格式和资源定义。

示例

需要创建三个 bicep 文件。 我们建议将所有三个文件保留在同一文件夹中。 也可将它们保存在不同的文件夹中,但请确保更新提到的 example.bicep 文件中的文件名的路径。

模板执行


az deployment group create --name samplebicep --template-file .\ds.bicep --resource-group  testrg --parameters '{}' --debug --subscription abc4def8-er57-2345-f4t8-dff65f67afr3

示例模板

example.bicep
metadata description = 'Demo Main'

module mrpmc1 './maintenanceconfig.bicep' = {

  name: 'mrpmc'

  params: {

   configName: 'test-MaintConfig-Bicep'

  }

}

module ca1 './config_assignment.bicep' = {

  name: 'mrpmcconfigassign2'

  scope: subscription()

  params : {

  configId: '${resourceGroup().id}/providers/microsoft.maintenance/maintenanceConfigurations/test-MaintConfig-Bicep'

  }

  dependsOn: [

    mrpmc1

  ]

}
maintenanceconfig.bicep
metadata description = 'Demo1'

param location string = 'China North 2'

param configName string = ''

targetScope = 'resourceGroup'

resource MaintConfig4 'Microsoft.Maintenance/maintenanceConfigurations@2023-04-01' = {

  name: configName //'test-MaintConfig-Bicep'

  location: location

  tags: null

  properties: {

    extensionProperties: {

      InGuestPatchMode: 'user'

    }

    installPatches: {

      linuxParameters: {

        classificationsToInclude: [

          'Critical'

          'Security'

        ]

        packageNameMasksToExclude: null

        packageNameMasksToInclude: null

      }

      rebootSetting: 'RebootIfRequired'

      windowsParameters: {

        classificationsToInclude: [

          'Critical'

          'Security'

        ]

      //  excludeKbsRequiringReboot: false

        kbNumbersToExclude: null

        kbNumbersToInclude: null

      }

    }

    maintenanceScope: 'InGuestPatch'

    maintenanceWindow: {

      duration: '02:00'

      expirationDateTime: '9999-12-31 23:59:59'

      recurEvery: 'Day'

      startDateTime: '2023-12-29 12:00'

      timeZone: 'Mountain Standard Time'

    }

  //  namespace: 'string'

   //visibility: 'public'

  }

}

output mrpConfId string = MaintConfig4.id
config_assignment.bicep
metadata description = 'Demo'

param location string = 'China North 2'

param configId string = ''

targetScope = 'subscription'

resource symbolicname 'Microsoft.Maintenance/configurationAssignments@2023-04-01' = {

  name: 'myconfig'

  location: 'global'

  scope: subscription()

  properties: {

    filter: {

      locations: []

      osTypes: [

        'Linux'

        'Windows'

      ]

     // resourceGroups: null

      resourceTypes: [

        'microsoft.compute/virtualmachines'

        'microsoft.hybridcompute/machines'

      ]

      resourceGroups:[

        'RG-MaintConfigs'

      ]

     // tagSettings:{

    //  filterOperator: 'All'

     //   tags: {updates: ['true']}

     // }

    }

    maintenanceConfigurationId: configId

    //resourceId: ''

   

  }

}
示例输出
{
  "id": "/subscriptions/eee2cef4-bc47-4278-b4f8-cfc65f25dfd8/resourceGroups/testrg/providers/Microsoft.Resources/deployments/samplebicep",
  "location": null,
  "name": "samplebicep",
  "properties": {
    "correlationId": "b125fc6f-f771-46d7-9b88-b31e0da959f5",
    "debugSetting": null,
    "dependencies": [
      {
        "dependsOn": [
          {
            "id": "/subscriptions/eee2cef4-bc47-4278-b4f8-cfc65f25dfd8/resourceGroups/testrg/providers/Microsoft.Resources/deployments/mrpmc",
            "resourceGroup": "testrg",
            "resourceName": "mrpmc",
            "resourceType": "Microsoft.Resources/deployments"
          }
        ],
        "id": "/subscriptions/eee2cef4-bc47-4278-b4f8-cfc65f25dfd8/providers/Microsoft.Resources/deployments/mrpmcconfigassign2",
        "resourceName": "mrpmcconfigassign2",
        "resourceType": "Microsoft.Resources/deployments"
      }
    ],
    "duration": "PT51.5773913S",
    "error": null,
    "mode": "Incremental",
    "onErrorDeployment": null,
    "outputResources": [
      {
        "id": "/subscriptions/eee2cef4-bc47-4278-b4f8-cfc65f25dfd8/providers/Microsoft.Maintenance/configurationAssignments/myconfig"
      },
      {
        "id": "/subscriptions/eee2cef4-bc47-4278-b4f8-cfc65f25dfd8/resourceGroups/testrg/providers/Microsoft.Maintenance/maintenanceConfigurations/test-MaintConfig-Bicep",
        "resourceGroup": "testrg"
      }
    ],
    "outputs": null,
    "parameters": null,
    "parametersLink": null,
    "providers": [
      {
        "id": null,
        "namespace": "Microsoft.Resources",
        "providerAuthorizationConsentState": null,
        "registrationPolicy": null,
        "registrationState": null,
        "resourceTypes": [
          {
            "aliases": null,
            "apiProfiles": null,
            "apiVersions": null,
            "capabilities": null,
            "defaultApiVersion": null,
            "locationMappings": null,
            "locations": [
              null,
              "canadacentral"
            ],
            "properties": null,
            "resourceType": "deployments",
            "zoneMappings": null
          }
        ]
      }
    ],
    "provisioningState": "Succeeded",
    "templateHash": "9584585695667229579",
    "templateLink": null,
    "timestamp": "2024-02-23T12:22:14.801966+00:00",
    "validatedResources": null
  },
  "resourceGroup": "testrg",
  "tags": null,
  "type": "Microsoft.Resources/deployments"
}

后续步骤

若要了解详细信息,请参阅 Azure 中虚拟机的维护