From 02d9336b320c39de652667069b7ecadd8ed36fc9 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Thu, 16 Feb 2023 16:30:23 +0100 Subject: Make using blocks as handlers a parser error (#79993) (#80010) Fixes #79968 (cherry picked from commit bd329dc54329a126056723311abd7442ed6a0389) --- changelogs/fragments/79968-blocks-handlers-error.yml | 2 ++ lib/ansible/playbook/helpers.py | 2 ++ test/integration/targets/handlers/runme.sh | 9 +++++++++ .../targets/handlers/test_block_as_handler-import.yml | 7 +++++++ .../targets/handlers/test_block_as_handler-include.yml | 8 ++++++++ .../test_block_as_handler-include_import-handlers.yml | 8 ++++++++ .../integration/targets/handlers/test_block_as_handler.yml | 13 +++++++++++++ test/units/playbook/test_helpers.py | 14 ++------------ 8 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 changelogs/fragments/79968-blocks-handlers-error.yml create mode 100644 test/integration/targets/handlers/test_block_as_handler-import.yml create mode 100644 test/integration/targets/handlers/test_block_as_handler-include.yml create mode 100644 test/integration/targets/handlers/test_block_as_handler-include_import-handlers.yml create mode 100644 test/integration/targets/handlers/test_block_as_handler.yml diff --git a/changelogs/fragments/79968-blocks-handlers-error.yml b/changelogs/fragments/79968-blocks-handlers-error.yml new file mode 100644 index 0000000000..4ad90daa4b --- /dev/null +++ b/changelogs/fragments/79968-blocks-handlers-error.yml @@ -0,0 +1,2 @@ +minor_changes: + - Make using blocks as handlers a parser error (https://github.com/ansible/ansible/issues/79968) diff --git a/lib/ansible/playbook/helpers.py b/lib/ansible/playbook/helpers.py index 444f571e99..38e32ef9a2 100644 --- a/lib/ansible/playbook/helpers.py +++ b/lib/ansible/playbook/helpers.py @@ -106,6 +106,8 @@ def load_list_of_tasks(ds, play, block=None, role=None, task_include=None, use_h raise AnsibleAssertionError('The ds (%s) should be a dict but was a %s' % (ds, type(ds))) if 'block' in task_ds: + if use_handlers: + raise AnsibleParserError("Using a block as a handler is not supported.", obj=task_ds) t = Block.load( task_ds, play=play, diff --git a/test/integration/targets/handlers/runme.sh b/test/integration/targets/handlers/runme.sh index 887a82db47..76fc99d85b 100755 --- a/test/integration/targets/handlers/runme.sh +++ b/test/integration/targets/handlers/runme.sh @@ -172,3 +172,12 @@ grep out.txt -e "handler ran" grep out.txt -e "after flush" ansible-playbook 79776.yml -i inventory.handlers "$@" + +ansible-playbook test_block_as_handler.yml "$@" 2>&1 | tee out.txt +grep out.txt -e "ERROR! Using a block as a handler is not supported." + +ansible-playbook test_block_as_handler-include.yml "$@" 2>&1 | tee out.txt +grep out.txt -e "ERROR! Using a block as a handler is not supported." + +ansible-playbook test_block_as_handler-import.yml "$@" 2>&1 | tee out.txt +grep out.txt -e "ERROR! Using a block as a handler is not supported." diff --git a/test/integration/targets/handlers/test_block_as_handler-import.yml b/test/integration/targets/handlers/test_block_as_handler-import.yml new file mode 100644 index 0000000000..ad6bb0d595 --- /dev/null +++ b/test/integration/targets/handlers/test_block_as_handler-import.yml @@ -0,0 +1,7 @@ +- hosts: localhost + gather_facts: false + tasks: + - debug: + handlers: + - name: handler + import_tasks: test_block_as_handler-include_import-handlers.yml diff --git a/test/integration/targets/handlers/test_block_as_handler-include.yml b/test/integration/targets/handlers/test_block_as_handler-include.yml new file mode 100644 index 0000000000..5b03b0a82e --- /dev/null +++ b/test/integration/targets/handlers/test_block_as_handler-include.yml @@ -0,0 +1,8 @@ +- hosts: localhost + gather_facts: false + tasks: + - command: echo + notify: handler + handlers: + - name: handler + include_tasks: test_block_as_handler-include_import-handlers.yml diff --git a/test/integration/targets/handlers/test_block_as_handler-include_import-handlers.yml b/test/integration/targets/handlers/test_block_as_handler-include_import-handlers.yml new file mode 100644 index 0000000000..61c058ba67 --- /dev/null +++ b/test/integration/targets/handlers/test_block_as_handler-include_import-handlers.yml @@ -0,0 +1,8 @@ +- name: handler + block: + - name: due to how handlers are implemented, this is correct as it is equivalent to an implicit block + debug: + - name: this is a parser error, blocks as handlers are not supported + block: + - name: handler in a nested block + debug: diff --git a/test/integration/targets/handlers/test_block_as_handler.yml b/test/integration/targets/handlers/test_block_as_handler.yml new file mode 100644 index 0000000000..bd4f5b991e --- /dev/null +++ b/test/integration/targets/handlers/test_block_as_handler.yml @@ -0,0 +1,13 @@ +- hosts: localhost + gather_facts: false + tasks: + - debug: + handlers: + - name: handler + block: + - name: due to how handlers are implemented, this is correct as it is equivalent to an implicit block + debug: + - name: this is a parser error, blocks as handlers are not supported + block: + - name: handler in a nested block + debug: diff --git a/test/units/playbook/test_helpers.py b/test/units/playbook/test_helpers.py index e784312f9b..a89730ca2e 100644 --- a/test/units/playbook/test_helpers.py +++ b/test/units/playbook/test_helpers.py @@ -160,20 +160,10 @@ class TestLoadListOfTasks(unittest.TestCase, MixinForMocks): self.assertIsInstance(block.always, list) self.assertEqual(len(block.always), 0) - def test_block_unknown_action_use_handlers(self): - ds = [{ - 'block': [{'action': 'foo_test_block_unknown_action'}] - }] - res = helpers.load_list_of_tasks(ds, play=self.mock_play, use_handlers=True, - variable_manager=self.mock_variable_manager, loader=self.fake_loader) - self._assert_is_task_list_or_blocks(res) - self.assertIsInstance(res[0], Block) - self._assert_default_block(res[0]) - - def test_one_bogus_block_use_handlers(self): + def test_block_use_handlers(self): ds = [{'block': True}] self.assertRaisesRegex(errors.AnsibleParserError, - "A malformed block was encountered", + "Using a block as a handler is not supported.", helpers.load_list_of_tasks, ds, play=self.mock_play, use_handlers=True, variable_manager=self.mock_variable_manager, loader=self.fake_loader) -- cgit v1.2.1