mirror of
https://github.com/haidaraM/ansible-playbook-grapher
synced 2025-02-20 05:28:24 +00:00
improvement: Hide instead of remove the plays without roles
This commit is contained in:
parent
5fca79ed0b
commit
8931b8eeb4
8 changed files with 44 additions and 18 deletions
|
@ -94,7 +94,6 @@ Comparison of the renderers:
|
|||
| Highlight on hover | ✅ | ❌ | ❌: NA |
|
||||
| Change graph orientation | ❌ | ✅ | ❌: NA |
|
||||
| Group roles by name | ✅ | ✅ | ✅: the roles with the same names will have the same IDs. |
|
||||
| Hide empty roles and blocks | ✅ | ✅ | ❌: They are kept in the JSON output |
|
||||
| View the output file in your the OS default viewer | ✅ | ✅ on https://mermaid.live/ | ✅ |
|
||||
| Tests of the output | Automatic | Manual (need a parser) | Automatic |
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ class PlaybookGrapherCLI(CLI):
|
|||
p.remove_empty_plays()
|
||||
|
||||
if self.options.hide_plays_without_roles:
|
||||
p.remove_plays_without_roles()
|
||||
p.hide_plays_without_roles()
|
||||
|
||||
if self.options.only_roles:
|
||||
p.hide_task_nodes()
|
||||
|
|
|
@ -492,15 +492,14 @@ class PlaybookNode(CompositeNode):
|
|||
for play in to_exclude:
|
||||
self.remove_node("plays", play)
|
||||
|
||||
def remove_plays_without_roles(self):
|
||||
"""Remove the plays that do not have roles from the playbook.
|
||||
def hide_plays_without_roles(self):
|
||||
"""Hide the plays that do not have at least one role.
|
||||
|
||||
:return:
|
||||
"""
|
||||
to_exclude = [play for play in self.plays if not play.has_node_type(RoleNode)]
|
||||
|
||||
for play in to_exclude:
|
||||
self.remove_node("plays", play)
|
||||
for play in self.plays:
|
||||
if not play.has_node_type(RoleNode):
|
||||
play.is_hidden = True
|
||||
|
||||
|
||||
class PlayNode(CompositeNode):
|
||||
|
|
|
@ -336,10 +336,11 @@ class GraphvizPlaybookBuilder(PlaybookBuilder):
|
|||
)
|
||||
|
||||
for play in self.playbook_node.plays:
|
||||
with self.digraph.subgraph(name=play.name) as play_subgraph:
|
||||
self.build_play(
|
||||
play, digraph=play_subgraph, show_handlers=show_handlers, **kwargs
|
||||
)
|
||||
if not play.is_hidden:
|
||||
with self.digraph.subgraph(name=play.name) as play_subgraph:
|
||||
self.build_play(
|
||||
play, digraph=play_subgraph, show_handlers=show_handlers, **kwargs
|
||||
)
|
||||
|
||||
return self.digraph.source
|
||||
|
||||
|
|
|
@ -199,7 +199,8 @@ class MermaidFlowChartPlaybookBuilder(PlaybookBuilder):
|
|||
self._indentation_level += 1
|
||||
|
||||
for play_node in self.playbook_node.plays:
|
||||
self.build_play(play_node, show_handlers=show_handlers, **kwargs)
|
||||
if not play_node.is_hidden:
|
||||
self.build_play(play_node, show_handlers=show_handlers, **kwargs)
|
||||
self._indentation_level -= 1
|
||||
|
||||
self.add_comment(f"End of the playbook '{self.playbook_node.display_name()}'\n")
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
- name: ansible_architecture
|
||||
debug:
|
||||
var: ansible_architecture
|
||||
tags:
|
||||
- test
|
||||
|
||||
- name: ansible_date_time
|
||||
debug:
|
||||
|
|
|
@ -89,8 +89,11 @@ def test_remove_plays_without_roles() -> None:
|
|||
playbook.add_node("plays", play_2)
|
||||
|
||||
assert len(playbook.plays) == 2, "There should be 2 plays"
|
||||
playbook.remove_plays_without_roles()
|
||||
assert len(playbook.plays) == 1, "There should be only one play"
|
||||
assert not play_1.is_hidden
|
||||
assert not play_2.is_hidden
|
||||
playbook.hide_plays_without_roles()
|
||||
assert not play_1.is_hidden
|
||||
assert play_2.is_hidden
|
||||
|
||||
|
||||
def test_get_all_tasks_nodes() -> None:
|
||||
|
|
|
@ -234,9 +234,9 @@ def test_with_block(request: pytest.FixtureRequest) -> None:
|
|||
def test_with_block_with_skip_tags(
|
||||
request: pytest.FixtureRequest,
|
||||
) -> None:
|
||||
"""
|
||||
:return:
|
||||
"""Test with the --skip-tags option with a block.
|
||||
|
||||
:return:
|
||||
"""
|
||||
json_path, playbook_paths = run_grapher(
|
||||
["with_block.yml"],
|
||||
|
@ -267,6 +267,7 @@ def test_with_block_with_skip_tags(
|
|||
def test_group_roles_by_name(request: pytest.FixtureRequest, flag: str) -> None:
|
||||
"""Test when grouping roles by name. This doesn't really affect the JSON renderer: multiple nodes will have the same ID.
|
||||
This test ensures that regardless of the flag '--group-roles-by-name', we get the same nodes in the output.
|
||||
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
|
@ -372,6 +373,27 @@ def test_handler_in_a_role(
|
|||
)
|
||||
|
||||
|
||||
def test_hide_plays_without_roles(request: pytest.FixtureRequest) -> None:
|
||||
"""Test the --hide-plays-without-roles flag.
|
||||
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
json_path, playbook_paths = run_grapher(
|
||||
["play-hiding.yml"],
|
||||
output_filename=request.node.name,
|
||||
additional_args=[
|
||||
"--hide-plays-without-roles",
|
||||
],
|
||||
)
|
||||
_common_tests(
|
||||
json_path,
|
||||
plays_number=2,
|
||||
roles_number=2,
|
||||
tasks_number=1,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("include_role_tasks_option", "expected_roles_number"),
|
||||
[("--", 4), ("--include-role-tasks", 6)],
|
||||
|
@ -384,7 +406,6 @@ def test_only_roles_with_nested_include_roles(
|
|||
) -> None:
|
||||
"""Test graphing a playbook with the --only-roles flag.
|
||||
|
||||
For the JSON renderer, the empty roles are not excluded by design. As a result, the number of roles is different
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
|
|
Loading…
Add table
Reference in a new issue