From 0a00263a535d4c8959afd93c815f549da2d72a06 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 2 Feb 2023 13:21:59 -0600 Subject: [PATCH] Use arg validation in debug action (#79866) * Use arg validation in debug action. Fixes #79862 * Add clog frag * Move the msg default into the argspec --- .../fragments/79862-debug-action-args.yml | 2 ++ lib/ansible/plugins/action/debug.py | 31 +++++++++++-------- 2 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 changelogs/fragments/79862-debug-action-args.yml diff --git a/changelogs/fragments/79862-debug-action-args.yml b/changelogs/fragments/79862-debug-action-args.yml new file mode 100644 index 00000000000..0b1dbc674de --- /dev/null +++ b/changelogs/fragments/79862-debug-action-args.yml @@ -0,0 +1,2 @@ +minor_changes: +- debug - Perform argspec valdiation in debug action plugin (https://github.com/ansible/ansible/issues/79862) diff --git a/lib/ansible/plugins/action/debug.py b/lib/ansible/plugins/action/debug.py index 2584fd3d5ec..0764a5ab57a 100644 --- a/lib/ansible/plugins/action/debug.py +++ b/lib/ansible/plugins/action/debug.py @@ -34,23 +34,28 @@ class ActionModule(ActionBase): if task_vars is None: task_vars = dict() - if 'msg' in self._task.args and 'var' in self._task.args: - return {"failed": True, "msg": "'msg' and 'var' are incompatible options"} + validation_result, new_module_args = self.validate_argument_spec( + argument_spec={ + 'msg': {'type': 'raw', 'default': 'Hello world!'}, + 'var': {'type': 'raw'}, + 'verbosity': {'type': 'int', 'default': 0}, + }, + mutually_exclusive=( + ('msg', 'var'), + ), + ) result = super(ActionModule, self).run(tmp, task_vars) del tmp # tmp no longer has any effect # get task verbosity - verbosity = int(self._task.args.get('verbosity', 0)) + verbosity = new_module_args['verbosity'] if verbosity <= self._display.verbosity: - if 'msg' in self._task.args: - result['msg'] = self._task.args['msg'] - - elif 'var' in self._task.args: + if new_module_args['var']: try: - results = self._templar.template(self._task.args['var'], convert_bare=True, fail_on_undefined=True) - if results == self._task.args['var']: + results = self._templar.template(new_module_args['var'], convert_bare=True, fail_on_undefined=True) + if results == new_module_args['var']: # if results is not str/unicode type, raise an exception if not isinstance(results, string_types): raise AnsibleUndefinedVariable @@ -61,13 +66,13 @@ class ActionModule(ActionBase): if self._display.verbosity > 0: results += u": %s" % to_text(e) - if isinstance(self._task.args['var'], (list, dict)): + if isinstance(new_module_args['var'], (list, dict)): # If var is a list or dict, use the type as key to display - result[to_text(type(self._task.args['var']))] = results + result[to_text(type(new_module_args['var']))] = results else: - result[self._task.args['var']] = results + result[new_module_args['var']] = results else: - result['msg'] = 'Hello world!' + result['msg'] = new_module_args['msg'] # force flag to make debug output module always verbose result['_ansible_verbose_always'] = True