Skip to content

Commit

Permalink
Make using blocks as handlers a parser error (#79993) (#80010)
Browse files Browse the repository at this point in the history
Fixes #79968

(cherry picked from commit bd329dc)
  • Loading branch information
mkrizek committed Feb 16, 2023
1 parent a082726 commit 02d9336
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
2 changes: 2 additions & 0 deletions 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)
2 changes: 2 additions & 0 deletions lib/ansible/playbook/helpers.py
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions test/integration/targets/handlers/runme.sh
Expand Up @@ -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."
@@ -0,0 +1,7 @@
- hosts: localhost
gather_facts: false
tasks:
- debug:
handlers:
- name: handler
import_tasks: test_block_as_handler-include_import-handlers.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
@@ -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:
13 changes: 13 additions & 0 deletions 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:
14 changes: 2 additions & 12 deletions test/units/playbook/test_helpers.py
Expand Up @@ -160,20 +160,10 @@ def _assert_default_block(self, block):
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)
Expand Down

0 comments on commit 02d9336

Please sign in to comment.