使用 Python 管理 Azure 资源

了解如何将 Azure Python 与 Azure 资源管理器 配合使用来管理 Azure 资源。 有关管理资源组,请参阅 使用 Python 管理 Azure 资源组

将资源部署到现有的资源组

可以使用 Python 直接部署 Azure 资源,也可以通过部署 Azure 资源管理器模板(ARM 模版)来创建 Azure 资源。

使用 Python 类部署资源

以下示例使用 StorageManagementClient.storage_accounts.begin_create 创建存储帐户。 存储帐户的名称在 Azure 中必须是唯一的。

import os
import random
from azure.identity import AzureCliCredential
from azure.mgmt.storage import StorageManagementClient

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

random_postfix = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz1234567890', k=13))
storage_account_name = "demostore" + random_postfix

storage_client = StorageManagementClient(credential, subscription_id)

storage_account_result = storage_client.storage_accounts.begin_create(
    "exampleGroup",
    storage_account_name,
    {
        "location": "chinanorth",
        "sku": {
            "name": "Standard_LRS"
        }
    }
)

部署模板

若要部署 ARM 模板,请使用 ResourceManagementClient.deployments.begin_create_or_update。 以下示例部署 远程模板。 该模板可创建存储帐户。

import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import DeploymentMode

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

resource_client = ResourceManagementClient(credential, subscription_id)

resource_group_name = input("Enter the resource group name: ")
location = input("Enter the location (i.e. chinanorth2): ")
template_uri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.storage/storage-account-create/azuredeploy.json"

rg_deployment_result = resource_client.deployments.begin_create_or_update(
    resource_group_name,
    "exampleDeployment",
    {
        "properties": {
            "templateLink": {
                "uri": template_uri
            },
            "parameters": {
                "location": {
                    "value": location
                },
            },
            "mode": DeploymentMode.incremental
        }
    }
)

部署资源组和资源

可以创建一个资源组,然后将资源部署到该组。 有关详细信息,请参阅 创建资源组并部署资源

将资源部署到多个订阅或资源组

通常情况下,将模板中的所有资源部署到单个资源组。 不过,在某些情况下,你可能希望将一组资源部署在一起但将其放置在不同的资源组或订阅中。 有关详细信息,请参阅 将 Azure 资源部署到多个订阅或资源组

删除资源

以下示例演示如何删除存储帐户。

import os
from azure.identity import AzureCliCredential
from azure.mgmt.storage import StorageManagementClient

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

storage_client = StorageManagementClient(credential, subscription_id)

resource_group_name = "demoGroup"
storage_account_name = "demostore"

storage_account = storage_client.storage_accounts.delete(
    resource_group_name,
    storage_account_name
)

有关 Azure 资源管理器如何订购删除资源的详细信息,请参阅 Azure 资源管理器资源组删除

移动资源

以下示例演示如何将存储帐户从一个资源组移到另一个资源组。

import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

resource_client = ResourceManagementClient(credential, subscription_id)

src_resource_group_name = "sourceGroup"
dest_resource_group_name = "destinationGroup"
storage_account_name = "demostore"

dest_resource_group = resource_client.resource_groups.get(dest_resource_group_name)

storage_account = resource_client.resources.get(
    src_resource_group_name, "Microsoft.Storage", "", "storageAccounts", storage_account_name, "2022-09-01"
)

move_result = resource_client.resources.begin_move_resources(
    src_resource_group_name,
    {
        "resources": [storage_account.id],
        "targetResourceGroup": dest_resource_group.id,
    }
)

有关详细信息,请参阅 将资源移动到新的资源组或订阅

锁定资源

锁定可以防止组织中的其他用户意外删除或修改关键资源,例如 Azure 订阅、资源组或资源。

以下示例锁定了网站,使其无法删除。

import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ManagementLockClient

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

lock_client = ManagementLockClient(credential, subscription_id)

lock_result = lock_client.management_locks.create_or_update_at_resource_level(
    "exampleGroup",
    "Microsoft.Web",
    "",
    "sites",
    "examplesite",
    "lockSite",
    {
        "level": "CanNotDelete"
    }
)

以下脚本获取存储帐户的所有锁:

import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.locks import ManagementLockClient

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

resource_client = ResourceManagementClient(credential, subscription_id)
lock_client = ManagementLockClient(credential, subscription_id)

resource_group_name = "demoGroup"
storage_account_name = "demostore"

resource = resource_client.resources.get_by_id(
    f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
    "2021-04-01"
)

locks = lock_client.management_locks.list_at_resource_level(
    resource_group_name,
    "Microsoft.Storage",
    "",
    "storageAccounts",
    storage_account_name
)

for lock in locks:
    print(f"Lock Name: {lock.name}, Lock Level: {lock.level}")

以下脚本删除了网站的锁定:

import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ManagementLockClient

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

lock_client = ManagementLockClient(credential, subscription_id)

lock_client.management_locks.delete_at_resource_level(
    "exampleGroup",
    "Microsoft.Web",
    "",
    "sites",
    "examplesite",
    "lockSite"
)

有关详细信息,请参阅 使用 Azure 资源管理器锁定资源

标记资源

标记有助于按逻辑方式组织资源组和资源。 有关信息,请参阅 使用标记来组织 Azure 资源

后续步骤