def main(): module = AnsibleModule( argument_spec={ 'bind_dn': dict(default=None), 'bind_pw': dict(default='', no_log=True), 'dn': dict(required=True), 'params': dict(type='dict'), 'server_uri': dict(default='ldapi:///'), 'start_tls': dict(default=False, type='bool'), 'state': dict( default='present', choices=['present', 'absent', 'exact']), 'attributes': dict(required=True, type='dict'), 'ordered': dict(default=False, type='bool'), 'validate_certs': dict(default=True, type='bool'), }, supports_check_mode=True, ) if not HAS_LDAP: module.fail_json( msg="Missing required 'ldap' module (pip install python-ldap)") # Update module parameters with user's parameters if defined if 'params' in module.params and isinstance(module.params['params'], dict): module.params.update(module.params['params']) # Remove the params module.params.pop('params', None) # Instantiate the LdapAttr object ldap = LdapAttr(module) state = module.params['state'] # Perform action if state == 'present': modlist = ldap.add() elif state == 'absent': modlist = ldap.delete() elif state == 'exact': modlist = ldap.exact() changed = False if len(modlist) > 0: changed = True if not module.check_mode: try: ldap.connection.modify_s(ldap.dn, modlist) except Exception as e: module.fail_json(msg="Attribute action failed.", details=to_native(e), exception=traceback.format_exc()) module.exit_json(changed=changed, modlist=modlist)
def main(): module = AnsibleModule( argument_spec=gen_specs( name=dict(type='str', required=True), params=dict(type='dict'), state=dict(type='str', default='present', choices=['absent', 'exact', 'present']), values=dict(type='raw', required=True), ), supports_check_mode=True, ) if not HAS_LDAP: module.fail_json(msg=missing_required_lib('python-ldap'), exception=LDAP_IMP_ERR) if module.params['params']: module.fail_json( msg= "The `params` option to ldap_attr was removed in since it circumvents Ansible's option handling" ) # Instantiate the LdapAttr object ldap = LdapAttr(module) state = module.params['state'] # Perform action if state == 'present': modlist = ldap.add() elif state == 'absent': modlist = ldap.delete() elif state == 'exact': modlist = ldap.exact() changed = False if len(modlist) > 0: changed = True if not module.check_mode: try: ldap.connection.modify_s(ldap.dn, modlist) except Exception as e: module.fail_json(msg="Attribute action failed.", details=to_native(e)) module.exit_json(changed=changed, modlist=modlist)
def main(): module = AnsibleModule( argument_spec=gen_specs( name=dict(type='str', required=True), params=dict(type='dict'), state=dict(type='str', default='present', choices=['absent', 'exact', 'present']), values=dict(type='raw', required=True), ), supports_check_mode=True, ) if not HAS_LDAP: module.fail_json(msg=missing_required_lib('python-ldap'), exception=LDAP_IMP_ERR) # Update module parameters with user's parameters if defined if 'params' in module.params and isinstance(module.params['params'], dict): module.params.update(module.params['params']) # Remove the params module.params.pop('params', None) # Instantiate the LdapAttr object ldap = LdapAttr(module) state = module.params['state'] # Perform action if state == 'present': modlist = ldap.add() elif state == 'absent': modlist = ldap.delete() elif state == 'exact': modlist = ldap.exact() changed = False if len(modlist) > 0: changed = True if not module.check_mode: try: ldap.connection.modify_s(ldap.dn, modlist) except Exception as e: module.fail_json(msg="Attribute action failed.", details=to_native(e)) module.exit_json(changed=changed, modlist=modlist)
def main(): module = AnsibleModule( argument_spec=gen_specs( attributes=dict(type='dict', required=True), ordered=dict(type='bool', default=False, required=False), state=dict(type='str', default='present', choices=['absent', 'exact', 'present']), ), supports_check_mode=True, ) if not HAS_LDAP: module.fail_json(msg=missing_required_lib('python-ldap'), exception=LDAP_IMP_ERR) # Instantiate the LdapAttr object ldap = LdapAttrs(module) state = module.params['state'] # Perform action if state == 'present': modlist = ldap.add() elif state == 'absent': modlist = ldap.delete() elif state == 'exact': modlist = ldap.exact() changed = False if len(modlist) > 0: changed = True if not module.check_mode: try: ldap.connection.modify_s(ldap.dn, modlist) except Exception as e: module.fail_json(msg="Attribute action failed.", details=to_native(e)) module.exit_json(changed=changed, modlist=modlist)
def main(): module = AnsibleModule( argument_spec={ 'attributes': dict(default={}, type='dict'), 'bind_dn': dict(), 'bind_pw': dict(default='', no_log=True), 'dn': dict(required=True), 'objectClass': dict(type='raw'), 'params': dict(type='dict'), 'server_uri': dict(default='ldapi:///'), 'start_tls': dict(default=False, type='bool'), 'state': dict(default='present', choices=['present', 'absent']), }, supports_check_mode=True, ) if not HAS_LDAP: module.fail_json( msg="Missing requried 'ldap' module (pip install python-ldap).") state = module.params['state'] # Chek if objectClass is present when needed if state == 'present' and module.params['objectClass'] is None: module.fail_json(msg="At least one objectClass must be provided.") # Check if objectClass is of the correct type if ( module.params['objectClass'] is not None and not ( isinstance(module.params['objectClass'], basestring) or isinstance(module.params['objectClass'], list))): module.fail_json(msg="objectClass must be either a string or a list.") # Update module parameters with user's parameters if defined if 'params' in module.params and isinstance(module.params['params'], dict): for key, val in module.params['params'].items(): if key in module.argument_spec: module.params[key] = val else: module.params['attributes'][key] = val # Remove the params module.params.pop('params', None) # Instantiate the LdapEntry object ldap = LdapEntry(module) # Get the action function if state == 'present': action = ldap.add() elif state == 'absent': action = ldap.delete() # Perform the action if action is not None and not module.check_mode: try: action() except Exception: e = get_exception() module.fail_json(msg="Entry action failed.", details=str(e)) module.exit_json(changed=(action is not None))
def main(): module = AnsibleModule( argument_spec={ 'attributes': dict(default={}, type='dict'), 'bind_dn': dict(), 'bind_pw': dict(default='', no_log=True), 'dn': dict(required=True), 'objectClass': dict(type='raw'), 'params': dict(type='dict'), 'server_uri': dict(default='ldapi:///'), 'start_tls': dict(default=False, type='bool'), 'state': dict(default='present', choices=['present', 'absent']), 'validate_certs': dict(default=True, type='bool'), }, supports_check_mode=True, ) if not HAS_LDAP: module.fail_json( msg="Missing required 'ldap' module (pip install python-ldap).") state = module.params['state'] # Check if objectClass is present when needed if state == 'present' and module.params['objectClass'] is None: module.fail_json(msg="At least one objectClass must be provided.") # Check if objectClass is of the correct type if ( module.params['objectClass'] is not None and not ( isinstance(module.params['objectClass'], string_types) or isinstance(module.params['objectClass'], list))): module.fail_json(msg="objectClass must be either a string or a list.") # Update module parameters with user's parameters if defined if 'params' in module.params and isinstance(module.params['params'], dict): for key, val in module.params['params'].items(): if key in module.argument_spec: module.params[key] = val else: module.params['attributes'][key] = val # Remove the params module.params.pop('params', None) # Instantiate the LdapEntry object ldap = LdapEntry(module) # Get the action function if state == 'present': action = ldap.add() elif state == 'absent': action = ldap.delete() # Perform the action if action is not None and not module.check_mode: try: action() except Exception as e: module.fail_json(msg="Entry action failed.", details=to_native(e), exception=traceback.format_exc()) module.exit_json(changed=(action is not None))
def main(): module = AnsibleModule( argument_spec=gen_specs( name=dict(type='str', required=True), params=dict(type='dict'), state=dict(type='str', default='present', choices=['absent', 'exact', 'present']), values=dict(type='raw', required=True), ), supports_check_mode=True, ) if not HAS_LDAP: module.fail_json(msg=missing_required_lib('python-ldap'), exception=LDAP_IMP_ERR) # For Ansible-2.9.x and below, allow the params module parameter with a warning if LooseVersion(module.ansible_version) < LooseVersion('2.10'): if module.params['params']: module.deprecate("The `params` option to ldap_attr will be removed in Ansible 2.10" " since it circumvents Ansible's option handling", version='2.10') # However, the bind_pw parameter contains a password so it **must** go through the normal # argument parsing even though removing it breaks backwards compat. if 'bind_pw' in module.params['params']: module.fail_json(msg="Using `bind_pw` with the `params` option has been disallowed since" " it is insecure. Use the `bind_pw` option directly. The `params`" " option will be removed in Ansible-2.10") # Update module parameters with user's parameters if defined module.params.update(module.params['params']) # Remove params itself module.params.pop('params', None) else: # For Ansible 2.10 and above if module.params['params']: module.fail_json(msg="The `params` option to ldap_attr was removed in Ansible-2.10 since" " it circumvents Ansible's option handling") # Instantiate the LdapAttr object ldap = LdapAttr(module) state = module.params['state'] # Perform action if state == 'present': modlist = ldap.add() elif state == 'absent': modlist = ldap.delete() elif state == 'exact': modlist = ldap.exact() changed = False if len(modlist) > 0: changed = True if not module.check_mode: try: ldap.connection.modify_s(ldap.dn, modlist) except Exception as e: module.fail_json(msg="Attribute action failed.", details=to_native(e)) module.exit_json(changed=changed, modlist=modlist)
state = module.params['state'] class LdapEntry(object): _connection = None def __init__(self, module): self.module = module ldap = LdapEntry(module) if state == 'present': action = ldap.add() self.module.params['attributes']['objectClass'] = (self.module.params['objectClass']) if self.module.params['state'] == 'present': self.attrs = self._load_attrs() def _load_attrs(self) attrs = {} for name, value in self.module.params['attributes'].items(): if name not in attrs: attrs[name] = [] if isinstance (value, list): attrs[name] = list(map(to_bytes, value))