Raise a proper error when include/import_role is used as a handler (#77807)

pull/75719/merge
Martin Krizek 3 years ago committed by GitHub
parent f6ac2b4f93
commit e9af6efee6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- Raise a proper error when ``include_role`` or ``import_role`` is used as a handler.

@ -275,6 +275,9 @@ def load_list_of_tasks(ds, play, block=None, role=None, task_include=None, use_h
task_list.append(t) task_list.append(t)
elif action in C._ACTION_ALL_PROPER_INCLUDE_IMPORT_ROLES: 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( ir = IncludeRole.load(
task_ds, task_ds,
block=block, block=block,

@ -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 -e "\[WARNING\]: Handler 'handler name with {{ test_var }}' is unusable"
[ "$(grep out.txt -ce 'handler ran')" = "0" ] [ "$(grep out.txt -ce 'handler ran')" = "0" ]
[ "$(grep out.txt -ce 'handler with var 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."

@ -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

@ -301,12 +301,14 @@ class TestLoadListOfTasks(unittest.TestCase, MixinForMocks):
def test_one_bogus_include_role_use_handlers(self): def test_one_bogus_include_role_use_handlers(self):
ds = [{'include_role': {'name': 'bogus_role'}, 'collections': []}] 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, self.assertRaises(errors.AnsibleError, helpers.load_list_of_tasks,
variable_manager=self.mock_variable_manager, ds,
loader=self.fake_role_loader) self.mock_play,
self.assertEqual(len(res), 1) True, # use_handlers
self._assert_is_task_list_or_blocks(res) self.mock_block,
self.mock_variable_manager,
self.fake_role_loader)
class TestLoadListOfRoles(unittest.TestCase, MixinForMocks): class TestLoadListOfRoles(unittest.TestCase, MixinForMocks):

Loading…
Cancel
Save