こんにちは、Aireです。
今回はAzureのCLIツールである「Azure CLI」をインストールする方法を簡単に紹介します。最新の情報や詳細は、以下の公式サイトを参照してください。

Azure CLIのインストール手順
Windowsの場合
- 「msiexec」コマンドを実行し、インストーラのダウンロードと実行を行います。旧バージョンをダウンロードする場合は、<バージョン>のところをダウンロード可能な実際のバージョン(例:2.45.0)に置き換えます(バージョン情報はAzure CLIのリリースノートを参照)。また、必要に応じて、「/quiet」オプション(インストール中のUIが表示されず、ユーザ操作が不要)や「/passive」オプション(インストール進行状況バーのみが表示され、ユーザ操作が不要)などを使用してください。
 
<実行コマンド>
| 
					 1 2 3 4 5  | 
						<最新バージョン> C:\> msiexec /i https://aka.ms/installazurecliwindows [/quiet][/passive][/q{n|b|r|f}] <旧バージョン> C:\> msiexec /i https://azcliprod.blob.core.windows.net/msi/azure-cli-<バージョン>.msi [/quiet][/passive][/q{n|b|r|f}]  | 
					
<実行例>
| 
					 1  | 
						C:\> msiexec /i https://azcliprod.blob.core.windows.net/msi/azure-cli-2.45.0.msi /quiet  | 
					
- 「az –version」コマンドを実行して、インストールが正常に完了したか確認します。
 
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  | 
						C:\> az --version azure-cli                         2.45.0 core                              2.45.0 telemetry                          1.0.8 Dependencies: msal                              1.20.0 azure-mgmt-resource             21.1.0b1 Python location 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe' Extensions directory 'C:\Users\<ユーザー名>\.azure\cliextensions' Python (Windows) 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:37:59) [MSC v.1933 32 bit (Intel)] Legal docs and information: aka.ms/AzureCliLegal Your CLI is up-to-date.  | 
					
Linux(RHEL / CentOS)の場合
RHEL 8/9などのdnfを使用するディストリビューションでは、Azure CLI用のRPMパッケージが提供されています。
- 「rpm」コマンドを実行し、Microsoftリポジトリキー(公開鍵)をインポートします。
 
<実行コマンド>
| 
					 1  | 
						$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc  | 
					
「rpm -qa gpg-pubkey」コマンドで、インストールされた公開鍵の一覧を表示できます。(余談ですが、Azure上でRHELを起動した際はその時点で当該鍵がインストールされていました)
| 
					 1 2  | 
						$ rpm -qa gpg-pubkey /* --qf "%{version}-%{release} %{summary}\n" | grep Microsoft be1229cf-5631588c gpg(Microsoft (Release signing) <gpgsecurity@microsoft.com>)  | 
					
- 「dnf」コマンドを実行し、「packages-microsoft-prod」パッケージをインストールします。
 
<RHEL 8 / CentOS Stream 8>
| 
					 1  | 
						$ sudo dnf install https://packages.microsoft.com/config/rhel/8/packages-microsoft-prod.rpm  | 
					
<RHEL 9 / CentOS Stream 9>
| 
					 1  | 
						$ sudo dnf install https://packages.microsoft.com/config/rhel/9.0/packages-microsoft-prod.rpm  | 
					
- 「dnf」コマンドを実行し、「azure-cli」パッケージをインストールします。
 
| 
					 1  | 
						$ sudo dnf install azure-cli  | 
					
- 「az –version」コマンドを実行して、インストールが正常に完了したか確認します。
 
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  | 
						$ az --version azure-cli                         2.45.0 core                              2.45.0 telemetry                          1.0.8 Dependencies: msal                              1.20.0 azure-mgmt-resource             21.1.0b1 Python location '/usr/bin/python3.9' Extensions directory '/home/<ユーザー名>/.azure/cliextensions' Python (Linux) 3.9.7 (default, Nov  9 2022, 15:33:03) [GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] Legal docs and information: aka.ms/AzureCliLegal Your CLI is up-to-date.  | 
					
macOSの場合
macOSの場合、「Homebrew」を使用してAzure CLIをインストールするのが簡単です。「Homebrew」がインストールされていない場合は、以下の記事を参考に「Homebrew」をインストールしてください。

- ターミナルで「brew install」コマンドを実行し、Azure CLIをインストールします。
 
| 
					 1  | 
						$ brew install azure-cli  | 
					
- 「az –version」コマンドを実行して、インストールが正常に完了したか確認します。
 
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  | 
						$ az --version azure-cli                         2.45.0 core                              2.45.0 telemetry                          1.0.8 Dependencies: msal                              1.20.0 azure-mgmt-resource             21.1.0b1 Python location '/opt/homebrew/Cellar/azure-cli/2.45.0/libexec/bin/python' Extensions directory '/Users/<ユーザー名>/.azure/cliextensions' Python (Darwin) 3.10.10 (main, Feb 16 2023, 02:49:39) [Clang 14.0.0 (clang-1400.0.29.202)] Legal docs and information: aka.ms/AzureCliLegal Your CLI is up-to-date.  | 
					
Azure CLIによるサインイン手順(az login)
Azure CLIのインストールが完了したら、「az login」コマンドでサインインを行います。
ウェブブラウザを使用したサインイン手順
Azure CLIをインストールした環境でウェブブラウザが使用できる場合は本手順を実行します。
ターミナルで「az login」コマンドを実行します。ウェブブラウザが開き、Azureのサインインページが表示されるのでサインインを行います。サインインに成功すると以下のようにAzureアカウント情報が表示されます。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  | 
						$ az login A web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`. [   {     "cloudName": "AzureCloud",     "homeTenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",     "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",     "isDefault": true,     "managedByTenants": [],     "name": "従量課金",     "state": "Enabled",     "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",     "user": {       "name": "<ユーザ名>",       "type": "user"     }   } ]  | 
					
<実行コマンド>
| 
					 1  | 
						$ az login --tenant xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  | 
					
別の端末のウェブブラウザを使用したサインイン手順
Azure CLIをインストールした環境でウェブブラウザが使用できない場合は本手順を実行します。本手順ではデバイスコードを生成し、そのデバイスコードを別の端末のウェブブラウズ上に入力することで、別の端末を通してサインインを行います。
- ターミナルで「az login」コマンドを実行します。その際、「–use-device-code」オプションを使用します。(RHELなどGUIがインストールされていない場合、オプションを省略しても本手順に誘導されます)
 - ウェブブラウザを使用できる環境でウェブブラウザを開き、デバイスログインページ(https://microsoft.com/devicelogin)を開きます。
 - 「az login」コマンド実行時に表示されたデバイスコードをデバイスログインページに入力し、Azure CLIへのサインインを行います。サインインに成功すると以下のようにAzureアカウント情報が表示されます。
 
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  | 
						$ az login --use-device-code To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code 123456789 to authenticate. [   {     "cloudName": "AzureCloud",     "homeTenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",     "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",     "isDefault": true,     "managedByTenants": [],     "name": "従量課金",     "state": "Enabled",     "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",     "user": {       "name": "<ユーザ名>",       "type": "user"     }   } ]  | 
					
Azure CLIのアンインストール手順
Windowsの場合
- [コントロールパネル]を開き、[プログラム]-[プログラムと機能]に移動します。
 - プログラムの一覧から「Microsoft Azure CLI」を選択し、「アンインストール」を実行します。
 - 以下のようにコマンドプロンプトで「rmdir」コマンドを実行し、Azure CLIの設定情報を削除します。「/s」は、フォルダ内のファイルやサブフォルダを含めて削除するためのオプションで、「/q」は、削除前の確認メッセージを表示しないオプションです。
 
| 
					 1  | 
						C:\> rmdir /s /q Users\<ユーザー名>\.azure  | 
					
Linux(RHEL / CentOS)の場合
- 「dnf」コマンドを実行し、インストールした「azure-cli」パッケージを削除します。
 
| 
					 1  | 
						$ sudo dnf remove azure-cli  | 
					
- 「dnf」コマンドを実行し、インストールした「packages-microsoft-prod」パッケージを削除します。
 
| 
					 1  | 
						$ sudo dnf remove packages-microsoft-prod  | 
					
- 「rpm -e」コマンドを実行し、Microsoftリポジトリキー(公開鍵)をアンインストールします。
 
<実行コマンド>
| 
					 1  | 
						$ sudo rpm -e --allmatches gpg-pubkey-<公開鍵>  | 
					
<実行例>
| 
					 1  | 
						$ sudo rpm -e --allmatches gpg-pubkey-be1229cf-5631588c  | 
					
「rpm -qa gpg-pubkey」コマンドで、Microsoftリポジトリキー(公開鍵)がアンインストールされたか確認できます。
| 
					 1  | 
						$ rpm -qa gpg-pubkey /* --qf "%{version}-%{release} %{summary}\n" | grep Microsoft  | 
					
- 「rm」コマンドを実行し、Azure CLI関連のデータを削除します。
 
| 
					 1  | 
						$ sudo rm -rf ~/.azure  | 
					
macOSの場合
- 「brew uninstall」コマンドを実行し、AWS CLIをアンインストールします。
 
| 
					 1 2  | 
						brew uninstall azure-cli Uninstalling /opt/homebrew/Cellar/azure-cli/2.45.0... (21,827 files, 595.8MB)  | 
					
- 「rm」コマンドを実行し、Azure CLI関連のデータを削除します。
 
| 
					 1  | 
						$ rm -rf ~/.azure  | 
					
以上、ここまで。

