此示例演示如何在 Python 脚本中使用 Azure SDK 管理库来执行两项任务:
- 列出 Azure 订阅中的所有资源组。
- 列出特定资源组中的资源。
本文中的所有命令在 Linux/macOS bash 和 Windows 命令行界面中的工作方式相同,除非另有说明。
本文后面列出了 等效的 Azure CLI 命令 。
1:设置本地开发环境
如果尚未安装,请设置可以运行此代码的环境。 下面是一些选项:
使用
venv
或所选工具配置 Python 虚拟环境。 可以在本地或 Azure Cloud Shell 中创建虚拟环境,并在其中运行代码。 请务必激活虚拟环境以开始使用它。 若要安装 python,请参阅 “安装 Python”。python -m venv .venv source .venv/bin/activate # Linux or macOS .venv\Scripts\activate # Windows
使用 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 CLI 登录到 Azure:
az login
将
AZURE_SUBSCRIPTION_ID
环境变量设置为订阅 ID。 (可以运行 az account show 命令并从输出中的属性获取订阅 IDid
):列出订阅中的所有资源组:
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