Skip to content

Commit

Permalink
fix: Blocks inside roles weren't added to the graph (#225)
Browse files Browse the repository at this point in the history
Related to #223
  • Loading branch information
haidaraM authored Dec 20, 2024
1 parent c0b5aa5 commit 25346de
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 5 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# 2.7.0 (unreleased)

## What's Changed

* fix: Blocks inside roles weren't added to the graph @haidaraM in https://github.com/haidaraM/ansible-playbook-grapher/pull/225
* feat: Add support for excluding specific roles in the graph view with `--exclude-roles` by @Eltryo in https://github.com/haidaraM/ansible-playbook-grapher/pull/219

**Full Changelog**: https://github.com/haidaraM/ansible-playbook-grapher/compare/v2.6.0...v2.7.0

# 2.6.0 (2024-12-16)

## What's Changed
Expand Down
2 changes: 1 addition & 1 deletion ansibleplaybookgrapher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
While you can use this package into another project, it is not primarily designed for that (yet).
"""

__version__ = "2.6.0"
__version__ = "2.7.0dev"
__prog__ = "ansible-playbook-grapher"
7 changes: 4 additions & 3 deletions ansibleplaybookgrapher/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,17 @@ def _include_tasks_in_blocks(
node_type: str,
play_vars: dict,
) -> None:
"""Recursively read all the tasks of the block and add it to the graph
:param parent_nodes: This a list of parent nodes. Each time, we see an include_role, the corresponding node is
"""Recursively read all the tasks of the block and add it to the graph.
:param parent_nodes: This is a list of parent nodes. Each time, we see an include_role, the corresponding node is
added to this list
:param current_play:
:param block:
:param play_vars:
:param node_type:
:return:
"""
if not block._implicit and block._role is None:
if Block.is_block(block.get_ds()):
# Here we have an explicit block. Ansible internally converts all normal tasks to Block
block_node = BlockNode(
str(block.name),
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@pytest.fixture(name="data_loader")
def fixture_data_loader() -> DataLoader:
"""Return an Ansible DataLoader.
"""Return an Ansible DataLoader.
:return:
"""
from ansible.parsing.dataloader import DataLoader
Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures/blocks_with_role.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: test
hosts: localhost
gather_facts: false
pre_tasks:
- name: Block of pre_tasks
block:
- name: Pre_task in a block
ansible.builtin.debug:
msg: msg from pre_task in a block
roles:
- role: blocks

tasks:
- name: Task with a msg
ansible.builtin.debug:
msg: msg from a task

- name: Block of tasks
block:
- name: Task in a block
ansible.builtin.debug:
msg: msg from a task in a block
16 changes: 16 additions & 0 deletions tests/fixtures/roles/blocks/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---

- name: Task with a msg in a role
ansible.builtin.debug:
msg: Msg from a task

- name: Block of tasks in a role
block:
- name: My task in a block in a role
ansible.builtin.debug:
msg: Msg from a task in a block in a role

- name: Import tasks_file
ansible.builtin.import_role:
name: blocks
tasks_from: tasks_with_block
10 changes: 10 additions & 0 deletions tests/fixtures/roles/blocks/tasks/tasks_with_block.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Task with a msg from a tasks_file of a role
ansible.builtin.debug:
msg: msg from a task from a tasks_file of a role

- name: Block in a tasks_file of a role
block:
- name: Task in a block in a tasks_file of a role
ansible.builtin.debug:
msg: "This is a msg from a task in a block in a tasks_file of a role"
32 changes: 32 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,38 @@ def test_block_parsing(grapher_cli: PlaybookGrapherCLI) -> None:
assert post_tasks[0].index == 6


@pytest.mark.parametrize("grapher_cli", [["blocks_with_role.yml"]], indirect=True)
def test_block_with_roles_parsing(grapher_cli: PlaybookGrapherCLI) -> None:
"""Test the parsing of a playbook with blocks and roles.
:param grapher_cli:
:return:
"""
parser = PlaybookParser(
grapher_cli.options.playbooks[0],
include_role_tasks=True,
)
playbook_node = parser.parse()
play = playbook_node.plays()[0]

assert len(play.pre_tasks) == 1
assert isinstance(play.pre_tasks[0], BlockNode)
assert len(play.pre_tasks[0].tasks) == 1

assert len(play.roles) == 1
role = play.roles[0]
assert len(role.tasks) == 4
assert isinstance(role.tasks[1], BlockNode)
assert len(role.tasks[1].tasks) == 1
assert isinstance(role.tasks[3], BlockNode)
assert len(role.tasks[3].tasks) == 1

assert len(play.tasks) == 2
assert isinstance(play.tasks[1], BlockNode)
assert len(play.tasks[1].tasks) == 1


@pytest.mark.parametrize("grapher_cli", [["multi-plays.yml"]], indirect=True)
@pytest.mark.parametrize(
(
Expand Down

0 comments on commit 25346de

Please sign in to comment.