diff --git a/Makefile b/Makefile index 0a5deddb..ba842b98 100644 --- a/Makefile +++ b/Makefile @@ -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/*) diff --git a/ansibleplaybookgrapher/parser.py b/ansibleplaybookgrapher/parser.py index 90053231..72463e1f 100644 --- a/ansibleplaybookgrapher/parser.py +++ b/ansibleplaybookgrapher/parser.py @@ -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( diff --git a/tests/fixtures/include_role.yml b/tests/fixtures/include_role.yml index b8966880..1ac54ba4 100644 --- a/tests/fixtures/include_role.yml +++ b/tests/fixtures/include_role.yml @@ -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 diff --git a/tests/fixtures/include_role_with_loop.yml b/tests/fixtures/include_role_with_loop.yml deleted file mode 100644 index 1bd2930c..00000000 --- a/tests/fixtures/include_role_with_loop.yml +++ /dev/null @@ -1,13 +0,0 @@ ---- -- hosts: localhost - connection: local - tags: - - play2 - tasks: - - name: Include role with loop - include_role: - name: '{{ item }}' - loop: - - fake_role - - display_some_facts - diff --git a/tests/test_parser.py b/tests/test_parser.py index b24b1999..18de6ec5 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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" @@ -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): """