此範例示範如何使用 Python 腳本中的 Azure SDK 管理連結庫來執行兩項工作:
- 列出 Azure 訂用帳戶中的所有資源群組。
- 列出特定資源群組內的資源。
本文中的所有命令在 Linux/macOS 的 bash 和 Windows 的命令殼中都可運作,除非特別註明。
本文稍後會列出 對等的 Azure CLI 命令 。
1:設定本機開發環境
如果您尚未設定環境,您可以在其中執行此程序代碼。 以下是一些選項:
- 使用
venv
或您選擇的工具設定 Python 虛擬環境。 若要開始使用虛擬環境,請務必加以啟用。 若要安裝 Python,請參閱 安裝 Python。
#!/bin/bash
# Create a virtual environment
python -m venv .venv
# Activate the virtual environment
source .venv/Scripts/activate # only required for Windows (Git Bash)
使用 conda 環境。 若要安裝 Conda,請參閱 安裝 Miniconda。
在 Visual Studio Code 或 GitHub Codespaces中使用 開發容器。
2:安裝 Azure 程式庫套件
使用下列內容建立名為 requirements.txt 的檔案:
azure-mgmt-resource
azure-identity
在啟動虛擬環境後的終端機或命令提示字元中,安裝必要套件:
pip install -r requirements.txt
3:撰寫程式代碼以使用資源群組
3a. 列出訂用帳戶中的資源群組
使用下列程式代碼建立名為 list_groups.py 的 Python 檔案。 註解會說明詳細資料:
# Import the needed credential and management objects from the libraries.
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
import os
# Acquire a credential object.
credential = DefaultAzureCredential()
# Retrieve subscription ID from environment variable.
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
# Obtain the management object for resources.
resource_client = ResourceManagementClient(credential, subscription_id)
# Retrieve the list of resource groups
group_list = resource_client.resource_groups.list()
# Show the groups in formatted output
column_width = 40
print("Resource Group".ljust(column_width) + "Location")
print("-" * (column_width * 2))
for group in list(group_list):
print(f"{group.name:<{column_width}}{group.location}")
3b. 列出特定資源群組內的資源
使用下列程式代碼建立名為 list_resources.py 的 Python 檔案。 批註會說明詳細數據。
根據預設,程式代碼會列出 「myResourceGroup」 中的資源。 若要使用不同的資源群組,請將 RESOURCE_GROUP_NAME
環境變數設定為所需的組名。
# Import the needed credential and management objects from the libraries.
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
import os
# Acquire a credential object.
credential = DefaultAzureCredential()
# Retrieve subscription ID from environment variable.
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
# Retrieve the resource group to use, defaulting to "myResourceGroup".
resource_group = os.getenv("RESOURCE_GROUP_NAME", "myResourceGroup")
# Obtain the management object for resources.
resource_client = ResourceManagementClient(credential, subscription_id)
# Retrieve the list of resources in "myResourceGroup" (change to any name desired).
# The expand argument includes additional properties in the output.
resource_list = resource_client.resources.list_by_resource_group(
resource_group, expand = "createdTime,changedTime")
# Show the groups in formatted output
column_width = 36
print("Resource".ljust(column_width) + "Type".ljust(column_width)
+ "Create date".ljust(column_width) + "Change date".ljust(column_width))
print("-" * (column_width * 4))
for resource in list(resource_list):
print(f"{resource.name:<{column_width}}{resource.type:<{column_width}}"
f"{str(resource.created_time):<{column_width}}{str(resource.changed_time):<{column_width}}")
程式代碼中的驗證
本文稍後會使用 Azure CLI 登入 Azure,以執行範例程序代碼。 如果您的帳戶具有在 Azure 訂用帳戶中建立及列出資源群組的許可權,程式代碼將會順利執行。
若要在生產腳本中使用這類程序代碼,您可以將環境變數設定為使用服務主體型方法進行驗證。 若要深入瞭解,請參閱 如何使用 Azure 服務驗證 Python 應用程式。 您必須在 Azure 中指派適當的角色,以確保服務主體有足夠的許可權在訂用帳戶中建立和列出資源群組;例如,訂用帳戶上的 參與者 角色。
程式代碼中使用的類別參考連結
4:執行腳本
如果您尚未登入 Azure,請使用 Azure CLI 登入 Azure:
az login
將
AZURE_SUBSCRIPTION_ID
環境變數設定為訂用帳戶標識碼。 (您可以執行 az account show 命令,並從輸出中的 屬性取得訂用帳戶識別碼id
:export AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
列出訂用帳戶中的所有資源群組:
python list_groups.py
列出資源群組中的所有資源:
python list_resources.py
根據預設,程式代碼會列出 「myResourceGroup」 中的資源。 若要使用不同的資源群組,請將
RESOURCE_GROUP_NAME
環境變數設定為所需的組名。
如需參考:對等的 Azure CLI 命令
下列 Azure CLI 命令會列出訂用帳戶中的資源群組:
az group list
下列命令會列出centralus區域中 「myResourceGroup」 內的資源( location
需要自變數才能識別特定的數據中心):
az resource list --resource-group myResourceGroup --location centralus