-
-
Save deardooley/6aedd44a0972fc8df799760ea6e1bc71 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash | |
# ldap2json.sh | |
# | |
# Example shell script showing how to parse LDIF output from the | |
# ldapsearch command into a json array. Please note that this | |
# script almost certainly does not handle every edge case, and | |
# is best used to pull a handful of fields a user or group | |
# record in a FreeIPA ldap server and format them as JSON for use | |
# in other tools. | |
# | |
LDAP_SEARCH_BIND_DN=${LDAP_SEARCH_BIND_DN:-'uid=readonly,dc=ipa,dc=example,dc=com'} | |
LDAP_SEARCH_BIND_PASS=${LDAP_SEARCH_BIND_PASS:-'password'} | |
LDAP_SEARCH_BASE=${LDAP_SEARCH_BASE:-'cn=users,dc=ipa,dc=example,dc=com'} | |
LDAP_SEARCH_HOST=${LDAP_SEARCH_HOST:-'ldap://ldap.ipa.example.com'} | |
LDAP_SEARCH_FILTER=${LDAP_SEARCH_FILTER:-'memberOf=cn=staff'} | |
LDAP_SEARCH_FIELDS=${LDAP_SEARCH_FIELDS:-'cn displayName mail uid'} | |
# Formats output from ldif format into a json structure containing the | |
# LDAP_SEARCH_FIELDS as keys. | |
# Note: | |
# - Repeated keys overwrite each other | |
# - uuid is used as a entry delimiter and will always be appended as the | |
# field provided in the ldapsearch request | |
function ldif_to_json () { | |
echo $(echo -e "${1}" | \ | |
sed -E 's/(.*)\: (.*)/"\1": "\2",/g' | \ | |
sed -E 's/"ssh\-rsa (.*) (.*)$/"ssh-rsa \1",/g' | \ | |
sed -E 's/^"dn(.*)$/{"dn\1/g' | \ | |
sed -E 's/^"uid(.*)",$/"uid\1"},/g' ) | \ | |
sed -E 's/(.*),$/[\1]/' | |
} | |
# Make the call to ldapsearch and send to the ldif_to_json | |
# function for formatting | |
ldif_to_json "$( ldapsearch -LLL -B -o ldif-wrap=no -x \ | |
-w "$LDAP_SEARCH_BIND_PASS" \ | |
-b "$LDAP_SEARCH_BASE" \ | |
-D "$LDAP_SEARCH_BIND_DN" \ | |
-H "$LDAP_SEARCH_HOST" \ | |
$LDAP_SEARCH_FILTER \ | |
$LDAP_SEARCH_FIELDS )" |
Hey, there's an aberrant ) on line 31. Otherwise, this looks good :)
@StoppingBuck Thanks for Answering it helped a little. I delete the ) on line 31 and add a ) to line 32 because the echo wasnt closed. Also delete the -B in line 37 because i got an error that is a unrecognized option. After this i also get an stderr like:
ansible-inventory 2.8.5
config file = /etc/ansible/ansible.cfg
configured module search path = ['/var/lib/awx/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible-inventory
python version = 3.6.8 (default, Nov 21 2019, 19:31:34) [GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /tmp/awx_6170_7jvavhkv/tmp6xuz8104 as it did not pass it's verify_file() method
auto declined parsing /tmp/awx_6170_7jvavhkv/tmp6xuz8104 as it did not pass it's verify_file() method
toml declined parsing /tmp/awx_6170_7jvavhkv/tmp6xuz8104 as it did not pass it's verify_file() method
[WARNING]: * Failed to parse /tmp/awx_6170_7jvavhkv/tmp6xuz8104 with script
plugin: failed to parse executable inventory script results from
/tmp/awx_6170_7jvavhkv/tmp6xuz8104: Syntax Error while loading YAML. did not
find expected ',' or '}' The error appears to be in '': line 1, column
54, but may be elsewhere in the file depending on the exact syntax problem.
File "/usr/lib/python3.6/site-packages/ansible/inventory/manager.py", line 268, in parse_source
plugin.parse(self._inventory, self._loader, source, cache=cache)
File "/usr/lib/python3.6/site-packages/ansible/plugins/inventory/script.py", line 161, in parse
raise AnsibleParserError(to_native(e))
[WARNING]: * Failed to parse /tmp/awx_6170_7jvavhkv/tmp6xuz8104 with yaml
plugin: Syntax Error while loading YAML. did not find expected The error appears to be in '/tmp/awx_6170_7jvavhkv/tmp6xuz8104': line
22, column 1, but may be elsewhere in the file depending on the exact syntax
problem. The offending line appears to be: function ldif_to_json () { ^ here
File "/usr/lib/python3.6/site-packages/ansible/inventory/manager.py", line 268, in parse_source
plugin.parse(self._inventory, self._loader, source, cache=cache)
File "/usr/lib/python3.6/site-packages/ansible/plugins/inventory/yaml.py", line 105, in parse
raise AnsibleParserError(e)
[WARNING]: * Failed to parse /tmp/awx_6170_7jvavhkv/tmp6xuz8104 with ini
plugin: /tmp/awx_6170_7jvavhkv/tmp6xuz8104:22: Expected key=value host variable
assignment, got: ldif_to_json
File "/usr/lib/python3.6/site-packages/ansible/inventory/manager.py", line 268, in parse_source
plugin.parse(self._inventory, self._loader, source, cache=cache)
File "/usr/lib/python3.6/site-packages/ansible/plugins/inventory/ini.py", line 138, in parse
raise AnsibleParserError(e)
[WARNING]: Unable to parse /tmp/awx_6170_7jvavhkv/tmp6xuz8104 as an inventory
source
ERROR! No inventory was parsed, please check your configuration and options.
Hello, i want to use your Script in awx to parse the ldif to json, for a dynamic inventory but i get:
ansible-inventory 2.8.5
config file = /etc/ansible/ansible.cfg
configured module search path = ['/var/lib/awx/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible-inventory
python version = 3.6.8 (default, Nov 21 2019, 19:31:34) [GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /tmp/awx_5736_3ezui8dn/tmpo07js48v as it did not pass it's verify_file() method
[ERROR]: /tmp/awx_5736_3ezui8dn/tmpo07js48v: line 27: ldif_to_json: command
not found
Maybe you can help me?