# LDAPSearch Ansible Playbook

**URL:** <https://forum.ansible.com/t/ldapsearch-ansible-playbook/2980>\
**Category:** Get Help\
**Created:** [December 19, 2023, 10:53am UTC](https://forum.ansible.com/t/ldapsearch-ansible-playbook/2980 "2023-12-19T10:53:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![emrehayta](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/emrehayta/32/1795_2.png) [@emrehayta](https://forum.ansible.com/u/emrehayta)\
**Post date:** [December 19, 2023, 10:53am UTC](https://forum.ansible.com/t/ldapsearch-ansible-playbook/2980/1 "2023-12-19T10:53:07Z")

</div>

Hello, I’m attempting an LDAP search using an Ansible playbook to list the usernames. Unfortunately, it’s not working as expected. Does anyone have any tips or insights regarding this?

My playbook:

```auto
---
- name: Query AD for User List
  hosts: localhost
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: Query AD for users
      community.general.ldap_search:
        server_uri: ldap://domain.xy:389
        dn: "OU=Mitarbeiter,OU=PM,DC=domain,DC=xy"
        bind_dn: "CN=SU_PM_AWX,OU=PM,DC=domain,DC=xy"
        bind_pw: "xyz"
        filter: "(objectClass=user)"
        #validate_certs: no
        #attrs:
          #- "sAMAccountName"
      register: ad_users

    - name: Display AD Users
      debug:
        var: ad_users

```

Output:

```auto
PLAY [Query AD for User List] ************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Query AD for users] ****************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Display AD Users] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "ad_users": {
        "changed": false,
        "failed": false,
        "results": []
    }
}

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

```

---

<div class="post-metadata">

**Author:** ![wa\_schaly](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wa_schaly/32/1079_2.png) [@wa\_schaly](https://forum.ansible.com/u/wa_schaly)\
**Post date:** [December 19, 2023, 11:17am UTC](https://forum.ansible.com/t/ldapsearch-ansible-playbook/2980/2 "2023-12-19T11:17:39Z")

</div>

Hallo Emre

Have you tried to use the -vvv Flag to get more debugging output.

Have you tried to set your ‘filter:’ line to one of the ad\_users you’re searching for, just to test if the LDAP server answers (correctly) at all

cheers  
Wolf-Agathon

---

<div class="post-metadata">

**Author:** ![emrehayta](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/emrehayta/32/1795_2.png) [@emrehayta](https://forum.ansible.com/u/emrehayta)\
**Post date:** [December 19, 2023, 12:05pm UTC](https://forum.ansible.com/t/ldapsearch-ansible-playbook/2980/3 "2023-12-19T12:05:24Z")

</div>

Hallo @wa_schaly ,

i tried it with -vvv but it shows not much more relevant information.  
I tried also to execute an ldapsearch command from the CLI with the same parameters and credentials and it works fine.

Kind Regards,  
Emre

---

<div class="post-metadata">

**Author:** ![wa\_schaly](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wa_schaly/32/1079_2.png) [@wa\_schaly](https://forum.ansible.com/u/wa_schaly)\
**Post date:** [December 20, 2023, 12:51pm UTC](https://forum.ansible.com/t/ldapsearch-ansible-playbook/2980/4 "2023-12-20T12:51:51Z")

</div>

Upsi - my question was not good enough 🙂

what I ment.  
can you delete the entire section ‘- name: Display AD Users’  
delete the line 'register: … ’  
and can you please set the filter: ‘(&(objectclass=user)(uid=some\_numeric\_existing\_UID))’

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [December 21, 2023, 4:12pm UTC](https://forum.ansible.com/t/ldapsearch-ansible-playbook/2980/5 "2023-12-21T16:12:17Z")

</div>

Hi,

Just to get this out of the way, are you getting results using `ldapsearch` (or any other CLI tools letting you query an LDAP directory) using the same filter ?
