Debian Codegraph @

lib/ansible/plugins/action/validate_argument_spec.py

raw
1 # Copyright 2021 Red Hat
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6 from ansible.errors import AnsibleError
7 from ansible.plugins.action import ActionBase
8 from ansible.module_utils.common.arg_spec import ArgumentSpecValidator
9 from ansible.utils.vars import combine_vars
10
11
12 class ActionModule(ActionBase):
13 """ Validate an arg spec"""
14
15 TRANSFERS_FILES = False
16 _requires_connection = False
17
18 def get_args_from_task_vars(self, argument_spec, task_vars):
19 """
20 Get any arguments that may come from `task_vars`.
21
22 Expand templated variables so we can validate the actual values.
23
24 :param argument_spec: A dict of the argument spec.
25 :param task_vars: A dict of task variables.
26
27 :returns: A dict of values that can be validated against the arg spec.
28 """
29 args = {}
30
31 for argument_name, argument_attrs in argument_spec.items():
32 if argument_name in task_vars:
33 args[argument_name] = task_vars[argument_name]
34 args = self._templar.template(args)
35 return args
36
37 def run(self, tmp=None, task_vars=None):
38 """
39 Validate an argument specification against a provided set of data.
40
41 The `validate_argument_spec` module expects to receive the arguments:
42 - argument_spec: A dict whose keys are the valid argument names, and
43 whose values are dicts of the argument attributes (type, etc).
44 - provided_arguments: A dict whose keys are the argument names, and
45 whose values are the argument value.
46
47 :param tmp: Deprecated. Do not use.
48 :param task_vars: A dict of task variables.
49 :return: An action result dict, including a 'argument_errors' key with a
50 list of validation errors found.
51 """
52 if task_vars is None:
53 task_vars = dict()
54
55 result = super(ActionModule, self).run(tmp, task_vars)
56 del tmp # tmp no longer has any effect
57
58 # This action can be called from anywhere, so pass in some info about what it is
59 # validating args for so the error results make some sense
60 result['validate_args_context'] = self._task.args.get('validate_args_context', {})
61
62 if 'argument_spec' not in self._task.args:
63 raise AnsibleError('"argument_spec" arg is required in args: %s' % self._task.args)
64
65 # Get the task var called argument_spec. This will contain the arg spec
66 # data dict (for the proper entry point for a role).
67 argument_spec_data = self._task.args.get('argument_spec')
68
69 # the values that were passed in and will be checked against argument_spec
70 provided_arguments = self._task.args.get('provided_arguments', {})
71
72 if not isinstance(argument_spec_data, dict):
73 raise AnsibleError('Incorrect type for argument_spec, expected dict and got %s' % type(argument_spec_data))
74
75 if not isinstance(provided_arguments, dict):
76 raise AnsibleError('Incorrect type for provided_arguments, expected dict and got %s' % type(provided_arguments))
77
78 args_from_vars = self.get_args_from_task_vars(argument_spec_data, task_vars)
79 validator = ArgumentSpecValidator(argument_spec_data)
80 validation_result = validator.validate(combine_vars(args_from_vars, provided_arguments), validate_role_argument_spec=True)
81
82 if validation_result.error_messages:
83 result['failed'] = True
84 result['msg'] = 'Validation of arguments failed:\n%s' % '\n'.join(validation_result.error_messages)
85 result['argument_spec_data'] = argument_spec_data
86 result['argument_errors'] = validation_result.error_messages
87 return result
88
89 result['changed'] = False
90 result['msg'] = 'The arg spec validation passed'
91
92 return result