Skip to content

Instantly share code, notes, and snippets.

@dch
Created March 11, 2017 11:30
Show Gist options
  • Select an option

  • Save dch/74173d987d22010c972c48cf1252cf14 to your computer and use it in GitHub Desktop.

Select an option

Save dch/74173d987d22010c972c48cf1252cf14 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# props to the non-functional version found on
# http://stackoverflow.com/questions/21721942/is-there-an-easy-way-to-generate-a-graph-of-ansible-role-dependencies
# thanks xmj@ for the tip
import sys
import pygraphviz
from glob import glob
import yaml
g = pygraphviz.AGraph()
role_nodes = {}
def add_role(role):
if role not in role_nodes:
g.add_node(role)
role_nodes[role] = pygraphviz.Node(g, role)
def link_roles(dependent, depended):
g.add_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.load(f.read())['dependencies']:
depended_role = dependency['role']
add_role(depended_role)
link_roles(dependent_role, depended_role)
g.layout()
g.draw('file.png', format='png',prog='dot')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment