JavaScriptを有効にしてください

Azure Arc 対応サーバーの Connected Machine Agent 更新を自動化し隊

 ·   5 分で読めます  ·   [Kento GitHub Copilot]

はじめに

本記事は GitHub Copilot を活用して作成しています。

Azure Arc 対応サーバーで利用する Connected Machine Agent の更新について検証しました。
Connected Machine Agent は Microsoft Update または手動(GUI、コマンドベース)での更新が必要ですが、エンタープライズ環境では Microsoft Update が採用しづらい場面があります。

そこで、Azure Automation の Hybrid Runbook Worker を使用してコマンドベースでのエージェント更新を自動化する方法を試してみました。

検証環境

  • Azure Arc 対応サーバー: Windows Server 2019 Standard
  • Azure Automation アカウント
  • Hybrid Runbook Worker: User Hybrid Runbook Worker V2
  • Connected Machine Agent: バージョン 1.45
image01
Connected Machine Agent のバージョン:

過去バージョンのエージェントのインストーラーを下記からダウンロードします
- What’s new with Azure Connected Machine agent - Azure Arc | Microsoft Learn
- Archive for What’s new with Azure Connected Machine agent - Azure Arc | Microsoft Learn

下記を実行し、エージェントをインストールします

1
msiexec /i AzureConnectedMachineAgent.msi /l*v installationlog.txt /qn | Out-String

下記を実行し 接続コマンドを実行します

1
2
3
4
5
6
7
8
$env:SUBSCRIPTION_ID = "xxxx";
$env:RESOURCE_GROUP = "xxx";
$env:TENANT_ID = "xxx";
$env:LOCATION = "southeastasia";
$env:AUTH_TYPE = "token";
$env:CLOUD = "AzureCloud";

& "$env:ProgramW6432\AzureConnectedMachineAgent\azcmagent.exe" connect --resource-group "$env:RESOURCE_GROUP" --tenant-id "$env:TENANT_ID" --location "$env:LOCATION" --subscription-id "$env:SUBSCRIPTION_ID" --cloud "$env:CLOUD";

Connected Machine Agent の更新方法

Connected Machine Agent には以下の更新方法があります:

  1. Microsoft Update(自動)
  2. 手動更新
    • GUI
    • コマンドライン

エンタープライズ環境では、以下の理由で Microsoft Update が採用されにくい場合があります:

  • 更新タイミングの制御が困難
  • セキュリティポリシーによる制約
  • 段階的なロールアウトの必要性
  • その他の Microsoft 製品の更新は別途管理したい

ざっくり手順

  1. Azure Automation アカウントの準備
  2. Hybrid Runbook Worker の構成
  3. Connected Machine Agent 更新用 Runbook の作成
  4. 更新スクリプトの実装
  5. 自動化の検証
  6. スケジュール実行の設定

詳細手順

1. Azure Automation アカウントの準備

Azure Portal から Azure Automation アカウントを作成します。

automation_account_create
Azure Automation アカウントの作成:

2. Hybrid Runbook Worker の構成

Azure Arc 対応サーバーを Hybrid Runbook Worker として構成します。
User Hybrid Runbook Worker V2 を使用することで、より簡単に設定できます。

詳細は Automation Hybrid Runbook Worker V2 を試し隊 – クラウドを勉強し隊 を見てください

hybrid_worker_setup
Hybrid Runbook Worker の設定:

3. Connected Machine Agent 更新用 Runbook の作成

PowerShell Runbook を作成して、Connected Machine Agent の更新を自動化します。

flowchart TD
    A[Runbook 実行開始] --> B["`現在のエージェント
                                バージョン確認`"]
    B --> D["`新しいエージェントを
                    ダウンロード`"]
    D --> F[エージェント更新実行]
    F --> G[更新結果の確認]
    G --> H[ログ出力]
    H --> I[Runbook 実行完了]

4. 更新スクリプトの実装

以下の PowerShell スクリプトを Runbook として実装します:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ログ記録関数
function Write-Log {
    param([string]$Message)
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    Write-Output "[$timestamp] $Message"
}

try {
    Write-Log "Connected Machine Agent 更新プロセスを開始します"
    
    # 現在のエージェントバージョンを確認
    $currentVersion = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Azure Connected Machine Agent" -Name "Version" -ErrorAction SilentlyContinue
    if ($currentVersion) {
        Write-Log "現在のエージェントバージョン: $($currentVersion.Version)"
    } else {
        Write-Log "Connected Machine Agent がインストールされていません"
        throw "エージェントが見つかりません"
    }
    
    # 最新バージョンの確認(簡略化)
    $downloadUrl = "https://aka.ms/AzureConnectedMachineAgent"
    $installerPath = "$env:TEMP\AzureConnectedMachineAgent.msi"
    
    # エージェントのダウンロード
    Write-Log "最新エージェントをダウンロード中..."
    try {
        Invoke-WebRequest -Uri $downloadUrl -OutFile $installerPath -UseBasicParsing
        Write-Log "ダウンロード完了: $installerPath"
    } catch {
        Write-Log "ダウンロードエラー: $($_.Exception.Message)"
        throw
    }
    
    # エージェントの更新実行
    Write-Log "Connected Machine Agent の更新を開始します"
    $updateArgs = "/i `"$installerPath`" /quiet /norestart"
    
    $process = Start-Process -FilePath "msiexec.exe" -ArgumentList $updateArgs -Wait -PassThru -NoNewWindow
    
    if ($process.ExitCode -eq 0) {
        Write-Log "エージェント更新が正常に完了しました"
        
        # 更新後のバージョン確認
        Start-Sleep -Seconds 10
        $newVersion = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Azure Connected Machine Agent" -Name "Version" -ErrorAction SilentlyContinue
        if ($newVersion) {
            Write-Log "更新後のエージェントバージョン: $($newVersion.Version)"
        }
        
        # サービスの状態確認
        $serviceStatus = Get-Service -Name "himds" -ErrorAction SilentlyContinue
        if ($serviceStatus -and $serviceStatus.Status -eq "Running") {
            Write-Log "Azure Connected Machine Agent サービスは正常に実行中です"
        } else {
            Write-Log "警告: Azure Connected Machine Agent サービスが実行されていません"
        }
        
    } else {
        Write-Log "エージェント更新に失敗しました。終了コード: $($process.ExitCode)"
        throw "更新プロセスが失敗しました"
    }
    
} catch {
    Write-Log "エラーが発生しました: $($_.Exception.Message)"
    throw
} finally {
    # 一時ファイルのクリーンアップ
    if (Test-Path $installerPath) {
        Remove-Item $installerPath -Force
        Write-Log "一時ファイルを削除しました"
    }
    Write-Log "Connected Machine Agent 更新プロセスを完了しました"
}

5. 自動化の検証

作成した Runbook をテスト実行して動作を確認します。
無事に最新バージョンにアップグレードされました

runbook_test
Runbook のテスト実行:
image03
Connected Machine Agent のバージョン:

もう一度実行して、最新バージョンがすでに適用されている場合の動作を確認しておきます
エラーをはかずに実行されて、結果的には無駄処理が行われてしまっています
ここは工夫をしたほうがよさそうです

image02
Runbook のテスト実行2:

6. スケジュール実行の設定

定期的な更新実行のためにスケジュールを設定します。

schedule_setup
スケジュール設定:

まとめ

Azure Automation の Hybrid Runbook Worker を使用することで、Connected Machine Agent の更新を効率的に自動化できました。

メリット

  • 更新タイミングの制御が可能
  • 複数サーバーの一括管理
  • 詳細なログ記録と監視
  • エラー時の自動リトライ

注意点

  • 更新前のバックアップ検討
  • 段階的なロールアウトの実施
  • 十分なテスト環境での検証

この方法により、エンタープライズ環境でも安全かつ確実に Connected Machine Agent を最新状態に保つことができます。

参考

共有

Kento
著者
[Kento GitHub Copilot]
2020年に新卒で IT 企業に入社. インフラエンジニア(主にクラウド)として活動中