From 4ed4d2d2800adc08a7dba9711acf277768b01ac7 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Thu, 11 Jun 2020 00:18:42 +0200 Subject: [PATCH] Validate args for includes in handlers too (#57537) (#69448) (cherry picked from commit 9645304da345a476a400b8d919852dae6a34f3b1) --- .../validate-include-args-in-handlers.yml | 2 ++ lib/ansible/playbook/handler_task_include.py | 7 ++++++- lib/ansible/playbook/task_include.py | 17 ++++++++++++++--- .../valid_include_keywords/playbook.yml | 3 ++- 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/validate-include-args-in-handlers.yml diff --git a/changelogs/fragments/validate-include-args-in-handlers.yml b/changelogs/fragments/validate-include-args-in-handlers.yml new file mode 100644 index 00000000000..4c55359286e --- /dev/null +++ b/changelogs/fragments/validate-include-args-in-handlers.yml @@ -0,0 +1,2 @@ +bugfixes: + - Validate include args in handlers. diff --git a/lib/ansible/playbook/handler_task_include.py b/lib/ansible/playbook/handler_task_include.py index 641e3338489..1c779f85d82 100644 --- a/lib/ansible/playbook/handler_task_include.py +++ b/lib/ansible/playbook/handler_task_include.py @@ -31,4 +31,9 @@ class HandlerTaskInclude(Handler, TaskInclude): @staticmethod def load(data, block=None, role=None, task_include=None, variable_manager=None, loader=None): t = HandlerTaskInclude(block=block, role=role, task_include=task_include) - return t.load_data(data, variable_manager=variable_manager, loader=loader) + handler = t.check_options( + t.load_data(data, variable_manager=variable_manager, loader=loader), + data + ) + + return handler diff --git a/lib/ansible/playbook/task_include.py b/lib/ansible/playbook/task_include.py index 59c77a8ef1f..ca89d0d6d65 100644 --- a/lib/ansible/playbook/task_include.py +++ b/lib/ansible/playbook/task_include.py @@ -58,13 +58,24 @@ class TaskInclude(Task): @staticmethod def load(data, block=None, role=None, task_include=None, variable_manager=None, loader=None): ti = TaskInclude(block=block, role=role, task_include=task_include) - task = ti.load_data(data, variable_manager=variable_manager, loader=loader) + task = ti.check_options( + ti.load_data(data, variable_manager=variable_manager, loader=loader), + data + ) - # Validate options + return task + + def check_options(self, task, data): + ''' + Method for options validation to use in 'load_data' for TaskInclude and HandlerTaskInclude + since they share the same validations. It is not named 'validate_options' on purpose + to prevent confusion with '_validate_*" methods. Note that the task passed might be changed + as a side-effect of this method. + ''' my_arg_names = frozenset(task.args.keys()) # validate bad args, otherwise we silently ignore - bad_opts = my_arg_names.difference(TaskInclude.VALID_ARGS) + bad_opts = my_arg_names.difference(self.VALID_ARGS) if bad_opts and task.action in ('include_tasks', 'import_tasks'): raise AnsibleParserError('Invalid options for %s: %s' % (task.action, ','.join(list(bad_opts))), obj=data) diff --git a/test/integration/targets/include_import/valid_include_keywords/playbook.yml b/test/integration/targets/include_import/valid_include_keywords/playbook.yml index a7ddeb4e92b..c70ec81fd15 100644 --- a/test/integration/targets/include_import/valid_include_keywords/playbook.yml +++ b/test/integration/targets/include_import/valid_include_keywords/playbook.yml @@ -1,7 +1,8 @@ - hosts: localhost gather_facts: false handlers: - - include_tasks: include_me_listen.yml + - include_tasks: + file: include_me_listen.yml listen: - include_me_listen