Skip to content

Commit

Permalink
import_tasks: Raise an error when invalid parameters specified
Browse files Browse the repository at this point in the history
Raise an error or a warning when user specifies parameters
which are not supported by import_tasks.

Fixes: ansible#64935

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Jul 6, 2020
1 parent 73139df commit acc30a0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/64935_import_tasks.yml
@@ -0,0 +1,2 @@
bugfixes:
- import_tasks - raise an error when invalid params are specified (https://github.com/ansible/ansible/issues/64935).
4 changes: 4 additions & 0 deletions lib/ansible/modules/import_tasks.py
Expand Up @@ -24,6 +24,10 @@
- If you need any of those to apply, use M(include_tasks) instead.
notes:
- This is a core feature of Ansible, rather than a module, and cannot be overridden like a module.
- Following are valid parameters - 'action', 'args', 'collections', 'debugger', 'delegate_to', 'delegate_facts',
'diff', 'environment', 'ignore_errors', 'ignore_unreachable', 'module_defaults', 'name', 'no_log', 'remote_user',
'run_once', 'tags', 'vars', and 'when'.
seealso:
- module: import_playbook
- module: import_role
Expand Down
17 changes: 13 additions & 4 deletions lib/ansible/playbook/task_include.py
Expand Up @@ -42,9 +42,11 @@ class TaskInclude(Task):
BASE = frozenset(('file', '_raw_params')) # directly assigned
OTHER_ARGS = frozenset(('apply',)) # assigned to matching property
VALID_ARGS = BASE.union(OTHER_ARGS) # all valid args
VALID_INCLUDE_KEYWORDS = frozenset(('action', 'args', 'collections', 'debugger', 'ignore_errors', 'loop', 'loop_control',
'loop_with', 'name', 'no_log', 'register', 'run_once', 'tags', 'vars',
'when'))
VALID_KEYWORDS = frozenset(('action', 'args', 'collections', 'debugger', 'ignore_errors', 'name', 'no_log',
'run_once', 'tags', 'vars', 'when'))
VALID_INCLUDE_KEYWORDS = VALID_KEYWORDS.union(frozenset(('loop', 'loop_control', 'loop_with', 'register',)))
VALID_IMPORT_KEYWORDS = VALID_KEYWORDS.union(frozenset(('delegate_to', 'delegate_facts', 'diff', 'environment',
'ignore_unreachable', 'module_defaults', 'remote_user')))

# =================================================================================
# ATTRIBUTES
Expand Down Expand Up @@ -94,7 +96,6 @@ def check_options(self, task, data):

def preprocess_data(self, ds):
ds = super(TaskInclude, self).preprocess_data(ds)

diff = set(ds.keys()).difference(self.VALID_INCLUDE_KEYWORDS)
for k in diff:
# This check doesn't handle ``include`` as we have no idea at this point if it is static or not
Expand All @@ -104,6 +105,14 @@ def preprocess_data(self, ds):
else:
display.warning("Ignoring invalid attribute: %s" % k)

if ds['action'] in ('import_tasks'):
diff = set(ds.keys()).difference(self.VALID_IMPORT_KEYWORDS)
for key in diff:
if key in ds and ds[key] is not Sentinel:
if C.INVALID_TASK_ATTRIBUTE_FAILED:
raise AnsibleParserError("'%s' is not a valid attribute for a %s" % (key, ds['action']), obj=ds)
else:
display.warning("Ignoring invalid attribute: '%s' for %s" % (key, ds['action']))
return ds

def copy(self, exclude_parent=False, exclude_tasks=False):
Expand Down

0 comments on commit acc30a0

Please sign in to comment.