【问题标题】:Ansible : 'NetworkManagementClient' object has no attribute 'private_endpoints'Ansible:“NetworkManagementClient”对象没有属性“private_endpoints”
【发布时间】:2021-11-20 11:16:12
【问题描述】:

我尝试使用 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
  • 可以从 Azure 门户手动创建专用终结点 相同的属性。
  • 子网的专用终结点网络策略 已经禁用
  • 我最初的 Ansible 版本是 2.9。出现错误后,我已将其更新为 Ansible 4.6,但仍然出现相同的错误。
  • 我的操作系统是 RHEL 8.4。
  • Python 3.6

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

【问题讨论】:

    标签: python azure ansible azure-storage rhel


    【解决方案1】:

    这个错误"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

    【讨论】:

    • 我应该把这段代码放在哪里?
    • 我收到 IndentationError: 你建议的 python 代码出现意外缩进
    • 我已经重新格式化了答案中的代码,可以参考azure_rm_privateendpoint_info.py
    猜你喜欢
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2019-06-25
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多