From e9af6efee6eb51b4caf89bf9a53ab80b38615e5d Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Tue, 17 May 2022 17:25:18 +0200 Subject: [PATCH] Raise a proper error when include/import_role is used as a handler (#77807) --- .../fragments/better-msg-role-in-handler.yml | 2 ++ lib/ansible/playbook/helpers.py | 3 +++ test/integration/targets/handlers/runme.sh | 4 ++++ .../targets/handlers/test_role_as_handler.yml | 11 +++++++++++ test/units/playbook/test_helpers.py | 14 ++++++++------ 5 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/better-msg-role-in-handler.yml create mode 100644 test/integration/targets/handlers/test_role_as_handler.yml diff --git a/changelogs/fragments/better-msg-role-in-handler.yml b/changelogs/fragments/better-msg-role-in-handler.yml new file mode 100644 index 00000000000..67fd0025b3f --- /dev/null +++ b/changelogs/fragments/better-msg-role-in-handler.yml @@ -0,0 +1,2 @@ +minor_changes: + - Raise a proper error when ``include_role`` or ``import_role`` is used as a handler. diff --git a/lib/ansible/playbook/helpers.py b/lib/ansible/playbook/helpers.py index ee48ab160b2..444f571e99b 100644 --- a/lib/ansible/playbook/helpers.py +++ b/lib/ansible/playbook/helpers.py @@ -275,6 +275,9 @@ def load_list_of_tasks(ds, play, block=None, role=None, task_include=None, use_h task_list.append(t) elif action in C._ACTION_ALL_PROPER_INCLUDE_IMPORT_ROLES: + if use_handlers: + raise AnsibleParserError(f"Using '{action}' as a handler is not supported.", obj=task_ds) + ir = IncludeRole.load( task_ds, block=block, diff --git a/test/integration/targets/handlers/runme.sh b/test/integration/targets/handlers/runme.sh index 7c403b4ea2a..0c3120ba3e6 100755 --- a/test/integration/targets/handlers/runme.sh +++ b/test/integration/targets/handlers/runme.sh @@ -114,3 +114,7 @@ grep out.txt -e "ERROR! The requested handler 'handler name with myvar' was not grep out.txt -e "\[WARNING\]: Handler 'handler name with {{ test_var }}' is unusable" [ "$(grep out.txt -ce 'handler ran')" = "0" ] [ "$(grep out.txt -ce 'handler with var ran')" = "0" ] + +# Test include_role and import_role cannot be used as handlers +ansible-playbook test_role_as_handler.yml "$@" 2>&1 | tee out.txt +grep out.txt -e "ERROR! Using 'include_role' as a handler is not supported." diff --git a/test/integration/targets/handlers/test_role_as_handler.yml b/test/integration/targets/handlers/test_role_as_handler.yml new file mode 100644 index 00000000000..ae67427408d --- /dev/null +++ b/test/integration/targets/handlers/test_role_as_handler.yml @@ -0,0 +1,11 @@ +- name: test include_role and import_role cannot be used as handlers + hosts: localhost + gather_facts: false + tasks: + - command: "echo" + notify: + - role_handler + handlers: + - name: role_handler + include_role: + name: doesnotmatter_fails_at_parse_time diff --git a/test/units/playbook/test_helpers.py b/test/units/playbook/test_helpers.py index a921a727ccd..63795a496b5 100644 --- a/test/units/playbook/test_helpers.py +++ b/test/units/playbook/test_helpers.py @@ -301,12 +301,14 @@ class TestLoadListOfTasks(unittest.TestCase, MixinForMocks): def test_one_bogus_include_role_use_handlers(self): ds = [{'include_role': {'name': 'bogus_role'}, 'collections': []}] - res = helpers.load_list_of_tasks(ds, play=self.mock_play, use_handlers=True, - block=self.mock_block, - variable_manager=self.mock_variable_manager, - loader=self.fake_role_loader) - self.assertEqual(len(res), 1) - self._assert_is_task_list_or_blocks(res) + + self.assertRaises(errors.AnsibleError, helpers.load_list_of_tasks, + ds, + self.mock_play, + True, # use_handlers + self.mock_block, + self.mock_variable_manager, + self.fake_role_loader) class TestLoadListOfRoles(unittest.TestCase, MixinForMocks):