【问题标题】:Azure python SDK - start or run VM from resource groupAzure python SDK - 从资源组启动或运行 VM
【发布时间】:2022-08-11 00:58:26
【问题描述】:

如果不使用 clientId 和 Secrete Id,我无法使用 python 代码启动 Azure Vm。

我们可以在不使用 client_id 和 secrete id 的情况下在 python 中启动或停止 Azure vm。

这是供参考的代码。

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient,ComputeManagementClientConfiguration

credentials = ServicePrincipalCredentials(
    client_id = \'<client-id>\',
    secret = \'<key>\',
    tenant = \'<tenant-id>\'
)

subscription_id = \'<subscription-id>\'

compute_config = ComputeManagementClientConfiguration(credentials, subscription_id, api_version=\'2015-05-01-preview\')
compute_client = ComputeManagementClient(compute_config)
resource_group_name = \'<resource-group>\'
vm_name = \'<vm-name>\'
result = compute_client.virtual_machines.deallocate(resource_group_name, vm_name)

在这里我们使用客户端 ID 和所有...但我想停止我的 Azure Vm 而不需要应用程序 ID/客户端 ID ..

    标签: python-3.x azure azure-devops azure-functions


    【解决方案1】:

    您可以为此使用 azure-identity 包和 DefaultAzureCredential:

    from azure.identity import DefaultAzureCredential
    credentials = DefaultAzureCredential()
    compute_config = ComputeManagementClientConfiguration(credentials, subscription_id, api_version='2015-05-01-preview')
    compute_client = ComputeManagementClient(compute_config)
    

    https://docs.microsoft.com/en-us/python/api/overview/azure/identity-readme?view=azure-python

    主要优势 - 你可以使用MSI authentication

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多