python - 使用 python 在 Azure 中部署资源

标签 python azure

我正在根据提供的模板使用 python 在 Azure 中部署资源。作为起点,我正在使用 https://github.com/Azure-Samples/Hybrid-Resource-Manager-Python-Template-Deployment

按原样使用,我在部署开始时遇到问题(deployer.py 部署函数)

def deploy(self, template, parameters):
  """Deploy the template to a resource group."""
  self.client.resource_groups.create_or_update(
  self.resourceGroup,
  {
    'location': os.environ['AZURE_RESOURCE_LOCATION']
  }
)

错误信息是

Message='ServicePrincipalCredentials' object has no attribute 'get_token'

说法正确,ServicePrincipalCredentials get_token 属性不存在,但 token 存在,可能是版本过时导致错误?

根据构造函数信息,错误可能出在凭据创建或客户端创建上

def __init__(self, subscription_id, resource_group, pub_ssh_key_path='~/id_rsa.pub'):
  mystack_cloud = get_cloud_from_metadata_endpoint(
    os.environ['ARM_ENDPOINT'])
  subscription_id = os.environ['AZURE_SUBSCRIPTION_ID'] //This may be an error as subscription_id is already provided as a parameter
  credentials = ServicePrincipalCredentials(
    client_id=os.environ['AZURE_CLIENT_ID'],
    secret=os.environ['AZURE_CLIENT_SECRET'],
    tenant=os.environ['AZURE_TENANT_ID'],
    cloud_environment=mystack_cloud
  ) --> here   
  self.subscription_id = subscription_id
  self.resource_group = resource_group
  self.dns_label_prefix = self.name_generator.haikunate()
  pub_ssh_key_path = os.path.expanduser(pub_ssh_key_path)
  # Will raise if file not exists or not enough permission
  with open(pub_ssh_key_path, 'r') as pub_ssh_file_fd:
    self.pub_ssh_key = pub_ssh_file_fd.read()
  self.credentials = credentials
  self.client = ResourceManagementClient(self.credentials, self.subscription_id,
    base_url=mystack_cloud.endpoints.resource_manager) --> here

你知道我该如何解决这个问题吗?

最佳答案

经过一番努力,我找到了解决方案。只需更换即可

credentials = ServicePrincipalCredentials(
    client_id=os.environ['AZURE_CLIENT_ID'],
    secret=os.environ['AZURE_CLIENT_SECRET'],
    tenant=os.environ['AZURE_TENANT_ID'],
    cloud_environment=mystack_cloud
  )

对于

self.credentials = DefaultAzureCredential()

最终代码如下:

from azure.identity import DefaultAzureCredential
def __init__(self, subscriptionId, resourceGroup):
  endpoints = get_cloud_from_metadata_endpoint(os.environ.get("ARM_ENDPOINT"))
  self.subscriptionId = subscriptionId
  self.resourceGroup = resourceGroup
  self.credentials = DefaultAzureCredential()
  self.client = ResourceManagementClient(self.credentials, self.subscriptionId,
  base_url=endpoints.endpoints.resource_manager)

关于python - 使用 python 在 Azure 中部署资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73884657/

相关文章:

python - 如何使用 Python 中的正则表达式获取最右边的字符加上双引号之间的文本

python - sklearn.model_selection.StratifiedShuffleSplit 中 n_splits 的用途是什么?

python - 如何从 Django Web 应用程序中在 linux shell 上启动 python 子进程命令?

Azure DevOps 托管构建代理 MSI

javascript - 有关使用客户端凭据 One Drive 访问 MS Graph 的问题吗?

python - pandas 数据框按特定值分组

python - 为什么我的简单 pytorch 网络不能在 GPU 设备上运行?

azure - 如何为现有 Azure WebApp 添加 Application Insights

azure - 部署 Web 作业时, "in-progress"作业会发生什么情况?

Azure DevOps Azure PowerShell 任务输出变量 - 无法将值传递到第三阶段