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)