Skip to content

Instantly share code, notes, and snippets.

@sboisson
Created June 30, 2020 11:26
Show Gist options
  • Save sboisson/472c6af5c7e15bc24114b13f11456190 to your computer and use it in GitHub Desktop.
Save sboisson/472c6af5c7e15bc24114b13f11456190 to your computer and use it in GitHub Desktop.
Use Graphviz to visualize roles dependencies
#!/usr/bin/env python3
import sys
import graphviz
from glob import glob
import yaml
import logging
logging.basicConfig(level=logging.DEBUG)
dot = graphviz.Digraph(name='roles')
role_nodes = {}
def add_role(role):
logging.info("Adding role: %s", role)
role_id = role.replace('/', '_')
if role not in role_nodes:
dot.node(role_id, role)
role_nodes[role] = role_id
def link_roles(dependent, depended):
logging.info("Adding dependency: %s -> %s", dependent, depended)
dot.edge(
role_nodes[dependent_role],
role_nodes[depended_role]
)
for path in glob('roles/*/meta/main.yml'):
dependent_role = path.split('/')[1]
add_role(dependent_role)
with open(path, 'r') as f:
for dependency in yaml.safe_load(f.read()).get('dependencies', []):
try:
if isinstance(dependency, str):
depended_role = dependency
else:
depended_role = dependency.get('role')
if depended_role:
add_role(depended_role)
link_roles(dependent_role, depended_role)
except:
logging.exception("Probleme with %s dependency: %s", dependent_role, dependency)
dot.format = 'png'
dot.render('roles.gv', view=True)
@sboisson
Copy link
Author

require graphviz package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment