JavaScriptを有効にしてください

Azure VM の実行コマンドを Bicep で試し隊

 ·   3 分で読めます  ·   Kento

Azure VM のゲスト OS 内にコマンドを実行する方法はいくつかあります
今回はその1つであるマネージド実行コマンドを Bicep で試してみます

実行コマンドについて

実行コマンドには2種類あります
Azure から OS にコマンドを発行する実行コマンド (RunCommand) 拡張機能について解説 | Japan Azure IaaS Core Support Blog のブログで違いについて書かれています

今回試すのは新しい方の “マネージド実行コマンド” です
先ほどのブログによると ARM テンプレートに対応しているということなので、Bicep でも使えるか試してみたいと思います

ざっくり手順

  1. Bicep ファイルの作成
  2. インライン スクリプト
  3. スクリプト URI
  4. 組み込みのスクリプト コマンド ID

1. Bicep ファイルの作成

Bicep でマネージドの実行コマンドを記述するとこのようになります

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Managed Run Command
resource runCommand 'Microsoft.Compute/virtualMachines/runCommands@2024-03-01' = {
  name: '${prefix}-runcommand'
  location: resourceGroup().location
  parent: vm
  properties: {
    source: {
      script: 'Install-WindowsFeature -Name Web-Server -IncludeManagementTools'
      //scriptUri: 'hogehoge'
      //commandId: 'DisableWindowsUpdate'
    }
  }
}

parent でコマンドを実行する VM を指定しています
実際に実行するコマンドは properties の中の source で指定します

Name 内容
script インライン スクリプト
scriptUri スクリプト URI
commandId 組み込みのスクリプト コマンド ID

3つの指定方法がありますが、複数種類を同時に使うことができないようです

1 回のコマンド実行でサポートされるソース入力は 1 種類だけです。

マネージド実行コマンドを使用して Azure の Windows VM でスクリプトを実行する - Azure Virtual Machines | Microsoft Learn

組み込みのスクリプト コマンド ID は アクション実行コマンドを使用して Azure Windows VM でスクリプトを実行する - Azure Virtual Machines | Microsoft Learn に記載があります

2. インライン スクリプト

インライン スクリプトで実行してみます
内容としては IIS のインストールです

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Managed Run Command
resource runCommand 'Microsoft.Compute/virtualMachines/runCommands@2024-03-01' = {
  name: '${prefix}-runcommand'
  location: resourceGroup().location
  parent: vm
  properties: {
    source: {
      script: 'Install-WindowsFeature -Name Web-Server -IncludeManagementTools'
    }
  }
}

無事にデプロイができたので VM に紐づけている Public IP アドレスにブラウザからアクセスしてみます
ちゃんと IIS のインストールが確認できました

image01
インライン スクリプト の確認:

3. スクリプト URI

続いて スクリプト URI で実行してみます
IIS の画面を少しカスタマイズしてみます

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Managed Run Command
resource runCommand 'Microsoft.Compute/virtualMachines/runCommands@2024-03-01' = {
  name: '${prefix}-runcommand'
  location: resourceGroup().location
  parent: vm
  properties: {
    source: {
      scriptUri: 'https://raw.githubusercontent.com/NakayamaKento/powershell_script/main/install_IIS.ps1'
    }
  }
}

デプロイ後、再度アクセスしてみるとちゃんと画面のカスタマイズができていました

image04
スクリプト URI の確認:

4. 組み込みのスクリプト コマンド ID

commandId でのデプロイを確認してみます
Windows Update の自動更新の無効化/有効化 を試してみます
Windows Update を無効化する ID を指定します

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Managed Run Command
resource runCommand 'Microsoft.Compute/virtualMachines/runCommands@2024-03-01' = {
  name: '${prefix}-runcommand'
  location: resourceGroup().location
  parent: vm
  properties: {
    source: {
      commandId: 'DisableWindowsUpdate'
    }
  }
}

デプロイ後、無効化されています

image03
DisableWindowsUpdate の確認:

有効化の実行もしてみます

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Managed Run Command
resource runCommand 'Microsoft.Compute/virtualMachines/runCommands@2024-03-01' = {
  name: '${prefix}-runcommand'
  location: resourceGroup().location
  parent: vm
  properties: {
    source: {
      commandId: 'EnableWindowsUpdate'
    }
  }
}

ちゃんと有効化されていました

image02
Windows Update の確認:

まとめ

マネージド実行コマンドを使ってみました
カスタムスクリプト拡張機能とは違い、拡張機能をインストールすることなく実行できました

今回は試しませんでしたが、複数のスクリプトを並行実行したり、実行時間が長いスクリプトが使えたりと痒い所に手が届くような機能になっています

参考

共有

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