def run(self, tmp=None, task_vars=None): """The std execution entry pt for an action plugin :param tmp: no longer used :type tmp: none :param task_vars: The vars provided when the task is run :type task_vars: dict :return: The results from the parser :rtype: dict """ valid, argspec_result, updated_params = check_argspec( DOCUMENTATION, "validate module", schema_conditionals=ARGSPEC_CONDITIONALS, **self._task.args ) if not valid: return argspec_result self._task_vars = task_vars self._playhost = ( task_vars.get("inventory_hostname") if task_vars else None ) self._validator_engine, validator_result = _load_validator( engine=updated_params["engine"], data=updated_params["data"], criteria=updated_params["criteria"], plugin_vars=task_vars, ) if validator_result.get("failed"): return validator_result try: result = self._validator_engine.validate() except AnsibleError as exc: raise AnsibleActionFail( to_text(exc, errors="surrogate_then_replace") ) except Exception as exc: raise AnsibleActionFail( "Unhandled exception from validator '{validator}'. Error: {err}".format( validator=self._validator_engine, err=to_text(exc, errors="surrogate_then_replace"), ) ) if result.get("errors"): self._result["errors"] = result["errors"] self._result.update({"failed": True}) if "msg" in result: self._result["msg"] = ( "Validation errors were found.\n" + result["msg"] ) else: self._result["msg"] = "Validation errors were found." else: self._result["msg"] = "all checks passed" return self._result
def validate(*args, **kwargs): if not len(args): raise AnsibleError( "Missing either 'data' value in test plugin input," "refer ansible.utils.validate test plugin documentation for details" ) params = {"data": args[0]} for item in ["engine", "criteria"]: if kwargs.get(item): params.update({item: kwargs[item]}) valid, argspec_result, updated_params = check_argspec( DOCUMENTATION, "validate test", schema_conditionals=ARGSPEC_CONDITIONALS, **params) if not valid: raise AnsibleError( "{argspec_result} with errors: {argspec_errors}".format( argspec_result=argspec_result.get("msg"), argspec_errors=argspec_result.get("errors"), )) validator_engine, validator_result = _load_validator( engine=updated_params["engine"], data=updated_params["data"], criteria=updated_params["criteria"], kwargs=kwargs, ) if validator_result.get("failed"): raise AnsibleError("validate lookup plugin failed with errors: %s" % validator_result.get("msg")) try: result = validator_engine.validate() except AnsibleError as exc: raise AnsibleError(to_text(exc, errors="surrogate_then_replace")) except Exception as exc: raise AnsibleError( "Unhandled exception from validator '{validator}'. Error: {err}". format( validator=updated_params["engine"], err=to_text(exc, errors="surrogate_then_replace"), )) errors = to_list(result.get("errors", [])) if len(errors): return False return True
def run(self, terms, variables, **kwargs): if len(terms) < 2: raise AnsibleLookupError( "missing either 'data' or 'criteria' value in lookup input," " refer ansible.utils.validate lookup plugin documentation for details" ) params = {"data": terms[0], "criteria": terms[1]} if kwargs.get("engine"): params.update({"engine": kwargs["engine"]}) valid, argspec_result, updated_params = check_argspec( DOCUMENTATION, "validate lookup", schema_conditionals=ARGSPEC_CONDITIONALS, **params) if not valid: raise AnsibleLookupError( "{argspec_result} with errors: {argspec_errors}".format( argspec_result=argspec_result.get("msg"), argspec_errors=argspec_result.get("errors"), )) validator_engine, validator_result = _load_validator( engine=updated_params["engine"], data=updated_params["data"], criteria=updated_params["criteria"], plugin_vars=variables, kwargs=kwargs, ) if validator_result.get("failed"): raise AnsibleLookupError( "validate lookup plugin failed with errors: {validator_result}" .format(validator_result=validator_result.get("msg"))) try: result = validator_engine.validate() except AnsibleError as exc: raise AnsibleLookupError( to_text(exc, errors="surrogate_then_replace")) except Exception as exc: raise AnsibleLookupError( "Unhandled exception from validator '{validator}'. Error: {err}" .format( validator=updated_params["engine"], err=to_text(exc, errors="surrogate_then_replace"), )) return to_list(result.get("errors", []))
def config_validator(): engine, result = _load_validator(engine="ansible.utils.config", data="", criteria=[]) return engine