Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use arg validation in debug action #79866

Merged
merged 3 commits into from Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions 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)
31 changes: 18 additions & 13 deletions lib/ansible/plugins/action/debug.py
Expand Up @@ -34,23 +34,28 @@ def run(self, tmp=None, task_vars=None):
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
Expand All @@ -61,13 +66,13 @@ def run(self, tmp=None, task_vars=None):
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
Expand Down