你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 GO 为虚拟机启用 Azure Automanage

注意

2027 年 9 月 30 日,Azure Automanage Best Practices 服务将停用。 因此,尝试创建新的配置文件或将新订阅加入服务将导致错误。 在此处详细了解如何在此日期之前迁移到 Azure Policy。

注意

从 2025 年 2 月 1 日起,Azure Automanage 将开始推出更改,以停止对依赖于已弃用的 Microsoft Monitoring Agent (MMA) 的所有服务的支持和强制执行。 若要继续使用更改跟踪和管理、VM 见解、更新管理和 Azure 自动化,请迁移到新的 Azure Monitor 代理 (AMA)

Azure Automanage 允许用户将 Azure 最佳做法无缝应用到其虚拟机。 本快速入门指南将帮助你使用 azure-sdk-for-go 存储库将最佳做法配置文件应用到现有虚拟机。

先决条件

注意

免费试用帐户无法访问本教程中使用的虚拟机。 请升级为即用即付订阅。

重要

你需要在包含你的虚拟机的资源组上具有参与者角色,才能启用 Automanage。 如果您是首次在某个订阅上启用 Automanage,则需要具有以下权限:该订阅的 所有者 角色,或同时具有 参与者用户访问管理员 角色。

安装所需程序包

此演示同时需要 Azure Identity 包和 Azure Automanage 包。

go get "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/automanage/armautomanage"
go get "github.com/Azure/azure-sdk-for-go/sdk/azidentity"

导入程序包

将 Azure Identity 包和 Azure Automanage 包导入脚本:

import (
	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/automanage/armautomanage"
)

向 Azure 进行身份验证并创建 Automanage 客户端

使用 Azure Identity 包向 Azure 进行身份验证,然后创建 Automanage 客户端:

credential, err := azidentity.NewDefaultAzureCredential(nil)
configProfilesClient, err := armautomanage.NewConfigurationProfilesClient("<subscription ID>", credential, nil)

为现有虚拟机启用最佳做法配置文件

configProfileId := "/providers/Microsoft.Automanage/bestPractices/AzureBestPracticesProduction"

properties := armautomanage.ConfigurationProfileAssignmentProperties{
    ConfigurationProfile: &configProfileId,
}

assignment := armautomanage.ConfigurationProfileAssignment{
    Properties: &properties,
}

// assignment name must be 'default'
newAssignment, err = assignmentClient.CreateOrUpdate(context.Background(), "default", "resourceGroupName", "vmName", assignment, nil)

后续步骤

通过访问 azure-sdk-for-go 存储库了解如何使用 GO Automanage 客户端执行更多操作。