diff --git a/changelogs/fragments/purge-empty-block.yml b/changelogs/fragments/purge-empty-block.yml new file mode 100644 index 00000000000..448f6b73d95 --- /dev/null +++ b/changelogs/fragments/purge-empty-block.yml @@ -0,0 +1,2 @@ +bugfixes: + - Do not keep empty blocks in PlayIterator after skipping tasks with tags. diff --git a/lib/ansible/playbook/block.py b/lib/ansible/playbook/block.py index 2bd7bd8a41e..4324a77ef54 100644 --- a/lib/ansible/playbook/block.py +++ b/lib/ansible/playbook/block.py @@ -371,7 +371,9 @@ class Block(Base, Conditional, CollectionSearch, Taggable): tmp_list = [] for task in target: if isinstance(task, Block): - tmp_list.append(evaluate_block(task)) + filtered_block = evaluate_block(task) + if filtered_block.has_tasks(): + tmp_list.append(filtered_block) elif (task.action == 'meta' or (task.action == 'include' and task.evaluate_tags([], self._play.skip_tags, all_vars=all_vars)) or task.evaluate_tags(self._play.only_tags, self._play.skip_tags, all_vars=all_vars)):