Share via


快速入門: 使用 Java 啟用虛擬機器的 Azure Automanage

Azure Automanage 可讓使用者順暢地將 Azure 最佳做法套用至其虛擬機器。 本快速入門指南將協助您使用 azure-sdk-for-java 存放庫將最佳做法組態設定檔套用至現有的虛擬機器。

必要條件

注意

免費試用帳戶無法存取本教學課程中使用的虛擬機器。 請升級為隨用隨付訂用帳戶。

重要

您必須在包含 VM 的資源群組上具有 [參與者] 角色,才能啟用 Automanage。 如果您要在訂用帳戶上初次啟用 Automanage,則需要下列權限:您訂用帳戶中的 [擁有者] 角色或 [參與者] 以及 [使用者存取管理員] 角色。

新增必要的相依性

Azure 身分識別Azure Automanage 相依性新增至 pom.xml

<!-- https://mvnrepository.com/artifact/com.azure/azure-identity -->
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.6.0-beta.1</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.azure.resourcemanager/azure-resourcemanager-automanage -->
<dependency>
    <groupId>com.azure.resourcemanager</groupId>
    <artifactId>azure-resourcemanager-automanage</artifactId>
    <version>1.0.0-beta.1</version>
</dependency>

向 Azure 進行驗證並建立 Automanage 用戶端

使用 [Azure 身分識別] 套件向 Azure 進行驗證,然後建立 Automanage 用戶端:

AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
TokenCredential credential = new DefaultAzureCredentialBuilder()
    .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
    .build();

AutomanageManager client = AutomanageManager
    .authenticate(credential, profile);

啟用現有虛擬機器的最佳做法組態設定檔

String configProfile = "/providers/Microsoft.Automanage/bestPractices/AzureBestPracticesProduction";

client
    .configurationProfileAssignments()
    .define("default") // name must be default
    .withExistingVirtualMachine("resourceGroupName", "vmName")
    .withProperties(
        new ConfigurationProfileAssignmentProperties()
            .withConfigurationProfile(configProfile))
    .create();

下一步

請瀏覽 azure-sdk-for-java 存放庫,了解如何使用 Java Automanage 用戶端執行更多作業。