Azure CLI は、Azure サブスクリプションの管理、管理グループの作成、サブスクリプションのロックに役立ちます。Azure 内に複数のサブスクリプションがある場合があります。 複数の組織の一部にすることも、組織が特定のリソースへのアクセスを複数のグループに分割することもできます。 Azure CLI では、グローバルとコマンドの両方でサブスクリプションを選択できます。
サブスクリプション、課金、およびコスト管理の詳細については、の課金とコスト管理に関するドキュメントを参照してください。
用語
テナントは、1 つの組織に関する情報が存在する Microsoft Entra ID のインスタンスです。 マルチテナント組織は、Microsoft Entra ID の複数のインスタンスを持つ組織です。 テナントには、1 つ以上 のサブスクリプション と ユーザーがあります。
ユーザーは、リソースを作成、管理、使用するために Azure にサインインするアカウントです。 1 人のユーザーが複数 のテナント とサブスクリプションにアクセス できる場合があります。
サブスクリプション は、Azure を含むクラウド サービスを使用するための Microsoft との契約です。 すべてのリソースはサブスクリプションに関連付けられます。 サブスクリプションにはリソース グループが含まれています。
Azure リソース グループ は、Azure ソリューションの関連リソースを保持するコンテナーです。 サブスクリプション内のリソース グループを管理する方法については、「Azure CLI を使用して Azure リソース グループを管理する方法」を参照してください。
アクティブなテナントを取得する
az account tenant list または az account show を使用して、アクティブなテナント ID を取得します。
az account tenant list
az account show
アクティブなテナントを変更する
テナントを切り替えるには、2 つのオプションがあります。
目的のテナント内でユーザーとしてサインインします。 az login を使用してアクティブなテナントを変更し、所属するサブスクリプションの一覧を更新します。
# sign in as a different user az login --user <myAlias@myCompany.com> --password <myPassword> # sign in with a different tenant az login --tenant <myTenantID>
組織で多要素認証が必要な場合は、
az login --user
を使用するときにこのエラーが発生する可能性があります。Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access...
別の
az login --tenant
コマンドを使用すると、HTTPS ページを開き、指定されたコードを入力するように求められます。 その後、多要素認証を使用して正常にサインインできます。 Azure CLI を使用したサインイン オプションの詳細については、「Azure CLI を使用したサインイン」を参照してください。
サブスクリプション情報を取得する
ほとんどの Azure CLI コマンドは、サブスクリプション内で動作します。 コマンドで --subscription
パラメーターを使用して、使用するサブスクリプションを指定できます。 サブスクリプションを指定しない場合、コマンドは現在アクティブなサブスクリプションを使用します。
現在使用しているサブスクリプションを表示したり、使用可能なサブスクリプションの一覧を取得したりするには、 az account show または az account list コマンドを実行します。 Azure CLI で Bash を使用する方法の詳細については、「これらのコマンドを使用する方法の例」を参照してください。
サブスクリプション情報を取得する方法を示す例を次に示します。
# get the current default subscription using show
az account show --output table
# get the current default subscription using list
az account list --query "[?isDefault]"
# get a subscription that contains search words or phrases
az account list --query "[?contains(name,'search phrase')].{SubscriptionName:name, SubscriptionID:id, TenantID:tenantId}" --output table
スクリプト内で使用するために、サブスクリプション情報を変数に格納することもできます。
# store the default subscription in a variable
subscriptionId="$(az account list --query "[?isDefault].id" --output tsv)"
echo $subscriptionId
# store a subscription of certain name in a variable
subscriptionId="$(az account list --query "[?name=='my case sensitive subscription full name'].id" --output tsv)"
echo $subscriptionId
ヒント
--output
パラメーターはグローバル パラメーターであり、すべてのコマンドで使用できます。
table 値を指定すると、出力がわかりやすい形式で表示されます。 詳細については、「Azure CLI コマンドの出力形式」をご覧ください。
アクティブなサブスクリプションを変更する
サブスクリプションには、名前と ID の両方があります。 目的のサブスクリプション ID または名前を指定する az account set を使用して、別のサブスクリプションに切り替えることができます。
# change the active subscription using the subscription name
az account set --subscription "My Demos"
# change the active subscription using the subscription ID
az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
変数を使用してサブスクリプションを変更することもできます。 次に例を示します。
# change the active subscription using a variable
subscriptionId="$(az account list --query "[?name=='my case sensitive subscription full name'].id" --output tsv)"
az account set --subscription $subscriptionId
別のテナント内のサブスクリプションに変更した場合は、アクティブなテナントも変更されます。 Microsoft Entra テナントに新しいサブスクリプションを追加する方法については、「Azure サブスクリプションを Microsoft Entra テナントに関連付けるまたは追加する」をご覧ください。
"...のサブスクリプションは存在しません。" というエラーが表示された場合、考えられる解決策についてはトラブルシューティングを参照してください。
サブスクリプション キャッシュをクリアする
サブスクリプションの一覧を更新するには、 az account clear コマンドを使用します。 更新された一覧を表示するには、もう一度サインインする必要があります。
az account clear
az login
サブスクリプション キャッシュのクリアは、技術的には Azure からログアウトするプロセスと同じではありません。 ただし、サブスクリプション キャッシュをクリアすると、再度サインインするまで、 az account set
を含む Azure CLI コマンドを実行することはできません。
Azure 管理グループを作成する
Azure 管理グループにはサブスクリプションが含まれています。 管理グループは、これらのサブスクリプションのアクセス、ポリシー、コンプライアンスを管理する方法を提供します。 詳細については、「 Azure 管理グループとは」を参照してください。
az account management-group コマンドを使用して、Azure 管理グループを作成および管理します。
az account management-group create コマンドを使用して、いくつかのサブスクリプションの管理グループを作成できます。
az account management-group create --name Contoso01
すべての管理グループを表示するには、 az account management-group list コマンドを使用します。
az account management-group list
az account management-group subscription add コマンドを使用して、新しいグループにサブスクリプションを追加します。
az account management-group subscription add --name Contoso01 --subscription "My Demos"
az account management-group subscription add --name Contoso01 --subscription "My Second Demos"
サブスクリプションを削除するには、 az account management-group subscription remove コマンドを 使用します。
az account management-group subscription remove --name Contoso01 --subscription "My Demos"
管理グループを削除するには、 az account management-group delete コマンドを 実行します。
az account management-group delete --name Contoso01
サブスクリプションを削除したり、管理グループを削除したりしても、サブスクリプションは削除または非アクティブ化されません。
Azure サブスクリプション ロックを設定する
管理者は、ユーザーがサブスクリプションを削除または変更できないように、サブスクリプションをロックする必要がある場合があります。 詳細については、「リソースをロックして予期しない変更を防ぐ」を参照してください。
Azure CLI では、 az account lock コマンドを使用します。 たとえば、 az account lock create コマンドを使用すると、ユーザーがサブスクリプションを削除できなくなります。
az account lock create --name "Cannot delete subscription" --lock-type CanNotDelete
注
ロックを作成または変更するには、サブスクリプションに対する contributor
アクセス許可が必要です。
サブスクリプションの現在のロックを表示するには、 az account lock list コマンドを使用します。
az account lock list --output table
アカウントを読み取り専用にした場合、結果は閲覧者ロールのアクセス許可をすべてのユーザーに割り当てるのと似ています。 個々のユーザーとロールのアクセス許可の設定については、 Azure CLI を使用した Azure ロールの割り当ての追加または削除に関するページを参照してください。
ロックの詳細を表示するには、 az account lock show コマンドを使用します。
az account lock show --name "Cannot delete subscription"
az account lock delete コマンドを使用して、ロックを削除できます。
az account lock delete --name "Cannot delete subscription"
トラブルシューティング
サブスクリプションが存在しない
文字体裁エラーに加えて、アクセス許可のタイミングの問題がある場合に、このエラーを受け取ることができます。 たとえば、 現在のターミナル ウィンドウが開いている間に新しいサブスクリプションへのアクセス許可が付与されている場合、このエラーが発生する可能性があります。 解決策は、ターミナル ウィンドウを閉じて再度開くか、 az logout
使用して az login
して、使用可能なサブスクリプションの一覧を更新することです。
サブスクリプションの検索と変更に役立つスクリプトを次に示します。
# See what subscription you are currently using.
az account show
# Get a list of available subscriptions.
az account list --output table
# If the subscription you are seeking is not in the list
# close and reopen your terminal window,
# or logout and then sign in again.
az logout
az login
# You can also clear your cache to refresh the
# available subscription list
az account clear
az login
# Did your available subscription list change?
az account list --output table
# If the subscription you are seeking is still not in the list,
# contact your system administrator. You cannot change your
# subscription to an ID that is not in the list.
# If the subscription you are seeking is now in the list,
# change your subscription.
az account set --subscription 00000000-0000-0000-0000-00000000000
こちらも参照ください
Azure CLI