Ansible:“NetworkManagementClient”对象没有属性“private_endpoints”

2024-09-22 10:25:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用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
  • 可以使用 相同的属性
  • 子网的专用终结点网络策略 已被禁用
  • 我最初的Ansible版本是2.9。错误发生后,我已将其更新为Ansible 4.6,但仍然得到相同的错误
  • 我的操作系统是RHEL8.4
  • Python 3.6

你知道这个错误的来源吗


Tags: rmnamelocalhost错误creategrouplinkansible
1条回答
网友
1楼 · 发布于 2024-09-22 10:25:00

此错误"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 a 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

相关问题 更多 >