Skip to content

Commit

Permalink
fix: do not fail if an include role with loop is found
Browse files Browse the repository at this point in the history
  • Loading branch information
haidaraM committed Jan 9, 2022
1 parent 17e43b7 commit 6fe0d28
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ANSIBLE_VERSION=2.10.7
VIRTUALENV_DIR=venv
VIRTUALENV_DIR=venv-test-install
PACKAGE := dist/$(shell ls dist 2> /dev/null)
SRC=$(wildcard ansibleplaybookgrapher/*.py setup.py ansibleplaybookgrapher/data/*)

Expand Down
27 changes: 16 additions & 11 deletions ansibleplaybookgrapher/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,32 @@ def _include_tasks_in_blocks(self, current_play: Play, parent_nodes: List[Compos
# Here we have an 'include_role'. The class IncludeRole is a subclass of TaskInclude.
# We do this because the management of an 'include_role' is different.
# See :func:`~ansible.playbook.included_file.IncludedFile.process_include_results` from line 155
self.display.v(
f"An 'include_role' found. Including tasks from the role '{task_or_block.get_name()}'")
self.display.v(f"An 'include_role' found. Including tasks from '{task_or_block.get_name()}'")

role_node = RoleNode(task_or_block.args['name'], raw_object=task_or_block)
role_node = RoleNode(task_or_block.get_name(), raw_object=task_or_block)
parent_nodes[-1].add_node(f"{node_type}s", EdgeNode(parent_nodes[-1], role_node,
convert_when_to_str(task_or_block.when)))

if self.include_role_tasks:
# If we have an include_role and we want to include role tasks, the parent node now becomes
# the role.
parent_nodes.append(role_node)

block_list, _ = task_or_block.get_block_list(play=current_play, loader=self.data_loader,
variable_manager=self.variable_manager)
if task_or_block.loop: # Looping on include_role is not supported
self.display.warning(
"Including role with loop is not supported for the moment. The include will not be "
"evaluated.")
block_list = []
else:
if self.include_role_tasks:
# If we have an include_role, and we want to include role tasks, the parent node now becomes
# the role.
parent_nodes.append(role_node)

block_list, _ = task_or_block.get_block_list(play=current_play, loader=self.data_loader,
variable_manager=self.variable_manager)
else:
self.display.v(f"An 'include_tasks' found. Including tasks from '{task_or_block.get_name()}'")

templar = Templar(loader=self.data_loader, variables=task_vars)
try:
included_file_path = handle_include_path(original_task=task_or_block, loader=self.data_loader,
templar=templar)
templar=templar)
except AnsibleUndefinedVariable as e:
# TODO: mark this task with some special shape or color
self.display.warning(
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/include_role.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@
debug:
msg: "Debug 1"
when: ansible_os == "ubuntu"

- name: (2) Include role
include_role:
name: fake_role

- name: (3) Debug 2
debug:
msg: "Debug 2"

- name: (4) Include role 2
when: x is not defined
include_role:
name: display_some_facts

- name: Include role with loop
include_role:
name: '{{ item }}'
loop:
- fake_role
- display_some_facts
13 changes: 0 additions & 13 deletions tests/fixtures/include_role_with_loop.yml

This file was deleted.

16 changes: 1 addition & 15 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_include_role_parsing(grapher_cli: PlaybookGrapherCLI, display: Display)
assert len(playbook_node.plays) == 1
play_node = playbook_node.plays[0].destination
tasks = play_node.tasks
assert len(tasks) == 4
assert len(tasks) == 5

# first task
assert tasks[0].destination.name == "(1) Debug"
Expand All @@ -56,20 +56,6 @@ def test_include_role_parsing(grapher_cli: PlaybookGrapherCLI, display: Display)
assert len(include_role_2.tasks) == 3


@pytest.mark.parametrize('grapher_cli', [["include_role_with_loop.yml"]], indirect=True)
def test_include_role_with_loop_parsing(grapher_cli: PlaybookGrapherCLI, display: Display):
"""
Test parsing of include_role with loop
:param grapher_cli:
:param display:
:return:
"""
parser = PlaybookParser(grapher_cli.options.playbook_filename, display=display, include_role_tasks=True)
playbook_node = parser.parse()
assert len(playbook_node.plays) == 1
play_node = playbook_node.plays[0].destination


@pytest.mark.parametrize('grapher_cli', [["with_block.yml"]], indirect=True)
def test_block_parsing(grapher_cli: PlaybookGrapherCLI, display: Display):
"""
Expand Down

0 comments on commit 6fe0d28

Please sign in to comment.