Repository: ansible-collections/community.general

Issue:
LDAP: Incorrect regex used for xorder_discovery parameter 
### Summary

in PR #6109 a new parameter `xorder_discovery` was added for the ldap modules. This is meant to detect whether the specified DN contains an "X order" component (e.g. "{0}"), and if not, perform a search to see if one already exists in the LDAP database (unless this parameter has been explicitly set to `disabled`).

The regex used to determine whether the specified DN contains an X order component or not is found in the function `_xorder_dn()` in `plugins/module_utils/ldap.py`. This regex assumes that the X order always follows an equals sign ("="), but this is not necessarily the case.  For example, module configurations in OpenLDAP look like this:

```
dn: cn=module{0},cn=config
objectClass: olcModuleList
cn: module{0}
olcModulePath: /usr/lib/ldap
olcModuleLoad: {0}back_mdb
structuralObjectClass: olcModuleList
```
This means that the `_find_dn()` function is called even when an X order component is present in the DN, and `xorder_discovery` is set to `auto`. Unsure if this has an practical consequences besides being incorrect behaviour.

### Issue Type

Bug Report

### Component Name

ldap_entry
ldap_attr
community/general/plugins/module_utils/ldap.py

### Ansible Version

```console (paste below)
$ ansible --version
ansible 2.10.8
  config file = None
  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
```


### Community.general Version

```console (paste below)
$ ansible-galaxy collection list community.general

# /home/ubuntu/.ansible/collections/ansible_collections
Collection        Version
----------------- -------
community.general 8.0.2

# /usr/lib/python3/dist-packages/ansible_collections
Collection        Version
----------------- -------
community.general 1.3.6
```


### Configuration

```console (paste below)
$ ansible-config dump --only-changed

```


### OS / Environment

Ubuntu 22.04

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
$ sudo slapcat -n -0

<snip>
dn: cn=module{1},cn=config
objectClass: olcModuleList
cn: module{1}
olcModulePath: /usr/lib/ldap
olcModuleLoad: {0}memberof
structuralObjectClass: olcModuleList
</snip>

$ cat slapd_module.yaml

- name: Demonstrate xorder_discovery's inability to notice list order components
  hosts: localhost
  connection: local
  tasks:
    - name: Load memberof module
      become: yes
      community.general.ldap_entry:
        xorder_discovery: auto
        dn: cn=module{1},cn=config
        objectClass:
          - olcModuleList
        attributes:
          cn: module
          olcModuleLoad: memberof
          olcmodulepath: /usr/lib/ldap

$ ansible-playbook ./slapd_module.yaml

PLAY [Demonstrate xorder_discovery's inability to notice list order components] **************************

TASK [Gathering Facts] ***********************************************************************************
ok: [localhost]

TASK [Load memberof module] ******************************************************************************
ok: [localhost]

PLAY RECAP ***********************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

### Expected Results

Since the specified DN contains an X order component, and `xorder_discovery` is set to `auto`, I would not expect the `LdapGeneric._find_dn()` function in `plugins/module_utils/ldap.py` to be called.

### Actual Results

The function is called (not shown).

### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
None

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
Files identified in the description:
* [`plugins/modules/ldap_attrs.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/ldap_attrs.py)
* [`plugins/modules/ldap_entry.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/ldap_entry.py)

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @drybjed @jtyr @noles
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/module_utils/ldap.py b/plugins/module_utils/ldap.py
--- a/plugins/module_utils/ldap.py
+++ b/plugins/module_utils/ldap.py
@@ -139,5 +139,7 @@ def _connect_to_ldap(self):
 
     def _xorder_dn(self):
         # match X_ORDERed DNs
-        regex = r"\w+=\{\d+\}.+"
-        return re.match(regex, self.module.params['dn']) is not None
+        regex = r".+\{\d+\}.+"
+        explode_dn = ldap.dn.explode_dn(self.module.params['dn'])
+
+        return re.match(regex, explode_dn[0]) is not None


Test patch: