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

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

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

先决条件

注意

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

重要

必须在包含 VM 的资源组上具有“参与者”角色,才能启用 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 客户端执行更多操作。