labunix's blog

labunixのラボUnix

az loginまわりを確認してみる

■az loginまわりを確認してみる。

$ az version --output table
Azure-cli    Azure-cli-core    Azure-cli-telemetry
-----------  ----------------  ---------------------
2.43.0       2.43.0            1.0.8

■az loginのオプションを確認
 特にグローバルオプション、デバッグの指定方法を確認しておく。

 lsecはawkを使ったセクション検索ツール
 https://github.com/labunix/lsec

$ az login --help | lsec Global
Global Arguments
    --debug                  : Increase logging verbosity to show all debug logs.
    --help -h                : Show this help message and exit.
    --only-show-errors       : Only show errors, suppressing warnings.
    --output -o              : Output format.  Allowed values: json, jsonc, none, table, tsv, yaml,
                               yamlc.  Default: json.
    --query                  : JMESPath query string. See http://jmespath.org/ for more information
                               and examples.
    --verbose                : Increase logging verbosity. Use --debug for full debug logs.

■追加のセキュリティオプションでMFAの有効化
 テナントを指定せずデフォルトディレクトリに接続するにはMFAが必要。
 そうでなければテナントIDを指定する。
 というエラーが出る。

The following tenants require Multi-Factor Authentication (MFA). Use 'az login --tenant TENANT_ID' to explicitly login to a tenant.
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX 'Default Directory'
No subscriptions found for mailaddress@examle.jp.

 対策はAzure Portalから辿るけど、実質MicrosoftアカウントのMFAを有効にすること。
 ※Azure ADのP1、有料オプションのMFAのことではないので、
  無料のMicrosoftアカウントのMFAをあえて無効のまま使い続けるのは、あまり良い選択ではない。

 (Azure Portal) サインイン中のアイコン -> Microsoftアカウント -> セキュリティ -> 追加のセキュリティ オプション -> 
 (Microsoft アカウント) 追加のセキュリティ -> 2段階認証 -> 有効にする

■Azure CLIでログイン
 システムの規定ブラウザを使用

 Azure CLI を使用してサインインする
 https://learn.microsoft.com/ja-jp/cli/azure/authenticate-azure-cli
 
$ az login

$ az account show
[
  {
    "cloudName": "AzureCloud",
"homeTenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "isDefault": true,
    "managedByTenants": [],
    "name": "Azure SubScription for labunix",
    "state": "Enabled",
"tenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "user": {
      "name": "mailaddress@example.jp",
      "type": "user"
    }
  }
]

■まだVMはない。

$ az vm list --output table

■まだリソースはない。

$ az resource list --output table

■リソースグループは古い残骸がある。

$ az group list  --output table
Name                               Location       Status
---------------------------------  -------------  ---------
MyRG                               japaneast      Succeeded
cloud-shell-storage-southeastasia  southeastasia  Succeeded

■リソースグループ削除用のコマンド生成
 最後にパイプでshに渡せすか、生成したコマンドを確認してコピペすれば実行できる。
 ステータスがSucceeded以外のリソースグループ行は無視する。

$ az group delete --help | lsec ^Arguments
Arguments
    --name --resource-group -g -n [Required] : Name of resource group. You can configure the default
                                               group using `az configure --defaults group=<name>`.
    --force-deletion-types -f                : The resource types you want to force delete.  Allowed
                                               values: Microsoft.Compute/virtualMachineScaleSets,
                                               Microsoft.Compute/virtualMachines.
    --no-wait                                : Do not wait for the long-running operation to finish.
    --yes -y                                 : Do not prompt for confirmation.

$ az group list  --output table | awk '($NF ~ /Succeeded/){print "az group delete --name \042"$1"\042 --yes --no-wait"}'
az group delete --name "MyRG" --yes --no-wait
az group delete --name "cloud-shell-storage-southeastasia" --yes --no-wait

■リージョン、ロケーションの一覧表示
 東西USと自国が見れれば大体良いかと。

$ az account list-locations --output table | awk -v keyword="West US" '(NR==1){print}($0 ~ keyword){print}'
DisplayName               Name                 RegionalDisplayName
West US 2                 westus2              (US) West US 2
West US 3                 westus3              (US) West US 3
West US (Stage)           westusstage          (US) West US (Stage)
West US 2 (Stage)         westus2stage         (US) West US 2 (Stage)
West US                   westus               (US) West US

$ az account list-locations --output table | awk -v keyword="East US" '(NR==1){print}($0 ~ keyword){print}'
DisplayName               Name                 RegionalDisplayName
East US                   eastus               (US) East US
East US 2                 eastus2              (US) East US 2
East US 2 EUAP            eastus2euap          (US) East US 2 EUAP
East US (Stage)           eastusstage          (US) East US (Stage)
East US 2 (Stage)         eastus2stage         (US) East US 2 (Stage)
East US STG               eastusstg            (US) East US STG

$ az account list-locations --output table | awk -v keyword="Japan" '(NR==1){print}($0 ~ keyword){print}'
DisplayName               Name                 RegionalDisplayName
Japan East                japaneast            (Asia Pacific) Japan East
Japan                     japan                Japan
Japan West                japanwest            (Asia Pacific) Japan West

■三つまとめて検索するには。
 or検索条件記号とsortコマンドへのパイプを追加する。

$ az account list-locations --output table | awk -v keyword="East US|West US|Japan" '(NR==1){print}($0 ~ keyword){print | "sort -V"}'
DisplayName               Name                 RegionalDisplayName
East US 2 EUAP            eastus2euap          (US) East US 2 EUAP
East US 2                 eastus2              (US) East US 2
East US 2 (Stage)         eastus2stage         (US) East US 2 (Stage)
East US STG               eastusstg            (US) East US STG
East US                   eastus               (US) East US
East US (Stage)           eastusstage          (US) East US (Stage)
Japan East                japaneast            (Asia Pacific) Japan East
Japan West                japanwest            (Asia Pacific) Japan West
Japan                     japan                Japan
West US 2                 westus2              (US) West US 2
West US 2 (Stage)         westus2stage         (US) West US 2 (Stage)
West US 3                 westus3              (US) West US 3
West US                   westus               (US) West US
West US (Stage)           westusstage          (US) West US (Stage)