示例:使用 Azure 库创建资源组

此示例演示如何使用 Python 脚本中的 Azure SDK 管理库创建资源组。 (The 本文稍后会提供等效的 Azure CLI 命令。如果想要使用Azure 门户,请参阅“创建资源组”。

除非另行说明,否则本文中的所有资源在 Linux/macOS bash 和 Windows 命令行界面上的工作方式相同。

1:设置本地开发环境

如果尚未安装,请设置可以运行此代码的环境。 提供以下选择:

2:安装 Azure 库包

创建一个具有以下内容的名为 requirements.txt 的文件:

azure-mgmt-resource
azure-identity

在激活了虚拟环境的终端或命令提示符下,安装下列要求:

pip install -r requirements.txt

3:编写代码以创建资源组

创建包含以下代码的名为“provision_rg.py” 的 Python 文件。 注释对详细信息进行了说明:

# Import the needed credential and management objects from the libraries.
import os

from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient

# Acquire a credential object using DevaultAzureCredential.
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)

# Provision the resource group.
rg_result = resource_client.resource_groups.create_or_update(
    "PythonAzureExample-rg", {"location": "centralus"}
)

# Within the ResourceManagementClient is an object named resource_groups,
# which is of class ResourceGroupsOperations, which contains methods like
# create_or_update.
#
# The second parameter to create_or_update here is technically a ResourceGroup
# object. You can create the object directly using ResourceGroup(location=
# LOCATION) or you can express the object as inline JSON as shown here. For
# details, see Inline JSON pattern for object arguments at
# https://learn.microsoft.com/azure/developer/python/sdk
# /azure-sdk-library-usage-patterns#inline-json-pattern-for-object-arguments

print(
    f"Provisioned resource group {rg_result.name} in the {rg_result.location} region"
)

# The return value is another ResourceGroup object with all the details of the
# new group. In this case the call is synchronous: the resource group has been
# provisioned by the time the call returns.

# To update the resource group, repeat the call with different properties, such
# as tags:
rg_result = resource_client.resource_groups.create_or_update(
    "PythonAzureExample-rg",
    {
        "location": "centralus",
        "tags": {"environment": "test", "department": "tech"},
    },
)

print(f"Updated resource group {rg_result.name} with tags")

# Optional lines to delete the resource group. begin_delete is asynchronous.
# poller = resource_client.resource_groups.begin_delete(rg_result.name)
# result = poller.result()

代码中的身份验证

本文稍后会使用 Azure CLI 登录到 Azure,以运行示例代码。 如果帐户有权在 Azure 订阅中创建和列出资源组,代码将成功运行。

若要在生产脚本中使用此类代码,可以将环境变量设置为使用基于服务主体的方法进行身份验证。 若要了解详细信息,请参阅 如何使用 Azure 服务对 Python 应用进行身份验证。 需要确保服务主体有足够的权限在订阅中创建和列出资源组,方法是在 Azure 中为其分配适当的角色;例如,订阅上的参与者角色。

4:运行脚本

  1. 如果尚未登录,请使用 Azure CLI 登录到 Azure:

    az login
    
  2. AZURE_SUBSCRIPTION_ID 环境变量设置为订阅 ID。 (可以运行 az account show 命令并从输出中的属性获取订阅 ID id ):

    set AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
    
  3. 运行以下脚本:

    python provision_rg.py
    

5:验证资源组

可以通过 Azure 门户或 Azure CLI 来验证该组是否存在。

  • Azure 门户:打开 Azure 门户,选择“资源组”,并查看是否列出了该组。 如果已打开门户,请使用 Refresh 命令更新该列表。

  • Azure CLI:使用 az group show 命令:

    az group show -n PythonAzureExample-rg
    

6:清理资源

如果不需要保留在此示例中创建的资源组,请运行 az group delete 命令。 资源组不会在订阅中产生任何持续费用,但资源组中的资源可能会继续产生费用。 最好清理未主动使用的任何组。 --no-wait 参数允许命令立即返回,而不是等到操作完成再返回。

az group delete -n PythonAzureExample-rg  --no-wait

你还可以使用 ResourceManagementClient.resource_groups.begin_delete 方法从代码中删除资源组。 本文脚本底部的注释代码演示了用法。

有关参考:等效的 Azure CLI 命令

以下 Azure CLI az group create 命令使用标记创建资源组,就像 Python 脚本一样:

az group create -n PythonAzureExample-rg -l centralus --tags "department=tech" "environment=test"

另请参阅