首页 > 解决方案 > Ansible:“NetworkManagementClient”对象没有属性“private_endpoints”

问题描述

我尝试使用Ansible为存储帐户部署私有端点。我正在使用azure.azcollection.azure_rm_privateendpoint。我的剧本代码如下:

- name: Create PE
  hosts: localhost
  connection: local
  tasks:

    - name: create_pe
      azure.azcollection.azure_rm_privateendpoint:
        name: testprivateendpoint
        resource_group: MYRG
        private_link_service_connections:
          - name: pe-satest
            group_ids: blob
            private_link_service_id: /subscriptions/xxxxxxxxx/resourceGroups/MYRG/providers/Microsoft.Storage/storageAccounts/mysa
        subnet:
          id: /subscriptions/xxxxxxx/resourceGroups/MYRG-VNET/providers/Microsoft.Network/virtualNetworks/MYVNET/subnets/mySubnet

启动 ansible-playbook 命令后,出现以下错误:

TASK [create_pe] ****************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Error creating or updating private endpoint testprivateendpoint - 'NetworkManagementClient' object has no attribute 'private_endpoints'"}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

关于这个错误的来源有什么想法吗?

标签: pythonazureansibleazure-storagerhel

解决方案


这个错误"msg": "Error creating or updating private endpoint testprivateendpoint - 'NetworkManagementClient' object has no attribute 'private_endpoints'"是由于NetworkManagementClient无法获取导致的private_endpoints

您可以尝试添加以下代码来解决它:

创建azure_rm_privateendpoint.py并将其包含在您的剧本中,例如main.yml. 您可以参考How to run a python script from an ansible playbookRunning Python script via ansibleRunning custom python script using ansible script module

def get_item(self):
    self.log('Get properties for {0}'.format(self.name))
    item = None
    results = []
        
    try:
        item = self.network_client.private_endpoints.get(self.resource_group, self.name)
    except Exception:
        self.log('Could not get info for @(Model.ModuleOperationNameUpper).')
        format_item = self.privateendpoints_to_dict(item)
        
    if format_item and self.has_tags(format_item['tags'], self.tags):
        results = [format_item]
    return results

def list_resource_group(self):
    self.log('List items for resource group')
    try:
        response = self.network_client.private_endpoints.list(self.resource_group)
    except CloudError as exc:
        self.fail("Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc)))

    results = []
    for item in response:
        format_item = self.privateendpoints_to_dict(item)
        if self.has_tags(format_item['tags'], self.tags):
            results.append(format_item)
    return results

def list_items(self):
    self.log('List all for items')
    try:
        response = self.network_client.private_endpoints.list_by_subscription()
    except CloudError as exc:
        self.fail("Failed to list all items - {0}".format(str(exc)))

    results = []
    for item in response:
        format_item = self.privateendpoints_to_dict(item)
        if self.has_tags(format_item['tags'], self.tags):
            results.append(format_item)
    return results

可以参考azure_rm_privateendpoint_info.pyazure_rm_privateendpoint.pyAzure rm privateendpoint

如果仍然遇到同样的错误,可以在 GitHub 上打开一个问题:ansible-collections/azure


推荐阅读