Changeset - 796d87f58565
[Not reviewed]
0 1 0
Branko Majic (branko) - 2 months ago 2024-09-07 14:48:33
branko@majic.rs
MAR-218: Fix the custom m_ldap_permissions module to work correctly with ansible-lint:

- In order for things to behave correctly, module should not invoke
the main part of code automatically during import, but only if run
via CLI.
- Without this the ansible-lint kept erroring out with:

"missing required arguments: filter, rules"
1 file changed with 9 insertions and 2 deletions:
0 comments (0 inline, 0 general)
roles/ldap_server/library/m_ldap_permissions.py
Show inline comments
 
#!/usr/bin/python3
 

	
 
from __future__ import (absolute_import, division, print_function)
 
from ansible.module_utils.basic import AnsibleModule
 

	
 
__metaclass__ = type
 

	
 
# Try to load the Python LDAP module.
 
try:
 
    import ldap
 
    import ldap.sasl
 
    import ldap.modlist
 
except ImportError:
 
    ldap_found = False
 
else:
 
    ldap_found = True
 

	
 

	
 
DOCUMENTATION = """
 
@@ -247,25 +250,25 @@ class LDAPPermissions(object):
 

	
 
        # Set-up the modification list.
 
        modify_list = self._get_modifications(database)
 

	
 
        # Apply modifications if necessary.
 
        if modify_list == []:
 
            return False
 
        else:
 
            self.connection.modify_s(database[0], modify_list)
 
            return True
 

	
 

	
 
def main():
 
def run_module():
 
    """
 
    Runs the module.
 
    """
 

	
 
    # Construct the module helper for parsing the arguments.
 
    module = AnsibleModule(
 
        argument_spec=dict(
 
            filter=dict(required=True),
 
            rules=dict(required=True, type='list'),
 
            server_uri=dict(required=False, default="ldapi:///"),
 
            bind_dn=dict(required=False, default=None),
 
            bind_password=dict(required=False, no_log=True)
 
@@ -289,14 +292,18 @@ def main():
 
    try:
 
        changed = ldap_permissions.update()
 

	
 
    except ldap.LDAPError as e:
 
        module.fail_json(msg="LDAP error: %s" % str(e))
 

	
 
    except DatabaseFilteringError as e:
 
        module.fail_json(msg="Module error: %s" % str(e))
 

	
 
    module.exit_json(changed=changed)
 

	
 

	
 
# Import module snippets.
 
def main():
 
    run_module()
 

	
 

	
 
if __name__ == '__main__':
 
    main()
0 comments (0 inline, 0 general)