From 683c467609badcf2a4874f00e7fcbb6c0a6beaa7 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Tue, 24 Dec 2019 10:26:32 +0530 Subject: [PATCH] Check if task file is specified for import_tasks (#57572) Signed-off-by: Abhijeet Kasurde --- .../54095-import_tasks-fix_no_task.yml | 2 ++ lib/ansible/playbook/task_include.py | 2 ++ .../tasks/test_include_tasks.yml | 10 --------- test/units/playbook/test_included_file.py | 22 +++++++++++++++++++ 4 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/54095-import_tasks-fix_no_task.yml diff --git a/changelogs/fragments/54095-import_tasks-fix_no_task.yml b/changelogs/fragments/54095-import_tasks-fix_no_task.yml new file mode 100644 index 0000000000..37f49ddbc2 --- /dev/null +++ b/changelogs/fragments/54095-import_tasks-fix_no_task.yml @@ -0,0 +1,2 @@ +minor_changes: +- Raise error when no task file is provided to import_tasks (https://github.com/ansible/ansible/issues/54095). diff --git a/lib/ansible/playbook/task_include.py b/lib/ansible/playbook/task_include.py index 59c77a8ef1..53c48afd7b 100644 --- a/lib/ansible/playbook/task_include.py +++ b/lib/ansible/playbook/task_include.py @@ -70,6 +70,8 @@ class TaskInclude(Task): if not task.args.get('_raw_params'): task.args['_raw_params'] = task.args.pop('file', None) + if not task.args['_raw_params']: + raise AnsibleParserError('No file specified for %s' % task.action) apply_attrs = task.args.get('apply', {}) if apply_attrs and task.action != 'include_tasks': diff --git a/test/integration/targets/include_import/tasks/test_include_tasks.yml b/test/integration/targets/include_import/tasks/test_include_tasks.yml index 7b769139c6..ebe2273e89 100644 --- a/test/integration/targets/include_import/tasks/test_include_tasks.yml +++ b/test/integration/targets/include_import/tasks/test_include_tasks.yml @@ -42,13 +42,3 @@ - name: include_tasks + action action: include_tasks tasks1.yml - - - name: test fail as expected without file - include_tasks: - ignore_errors: yes - register: res - - - name: verify fail as expected without file - assert: - that: - - res.msg == 'No include file was specified to the include' diff --git a/test/units/playbook/test_included_file.py b/test/units/playbook/test_included_file.py index d73415c698..511f3169e6 100644 --- a/test/units/playbook/test_included_file.py +++ b/test/units/playbook/test_included_file.py @@ -31,6 +31,7 @@ from ansible.playbook.task_include import TaskInclude from ansible.executor import task_result from ansible.playbook.included_file import IncludedFile +from ansible.errors import AnsibleParserError @pytest.fixture @@ -167,3 +168,24 @@ def test_process_include_simulate_free(mock_iterator, mock_variable_manager): assert res[0]._vars == {} assert res[1]._vars == {} + + +def test_empty_raw_params(): + parent_task_ds = {'debug': 'msg=foo'} + parent_task = Task.load(parent_task_ds) + parent_task._play = None + + task_ds_list = [ + { + 'include': '' + }, + { + 'include_tasks': '' + }, + { + 'import_tasks': '' + } + ] + for task_ds in task_ds_list: + with pytest.raises(AnsibleParserError): + TaskInclude.load(task_ds, task_include=parent_task)