Is there an easy way to generate a graph of Ansible role dependencies?

Since version 1.3, Ansible has supported role dependencies to encourage reuse of role definitions. To audit and maintain larger orchestrations, it would be nice to have some way to easily generate a dependency graph of which roles depend on which other roles.

An example of dependency definitions might be roles/app_node/meta/main.yml:

  - { role: common, some_parameter: 3 }
  - { role: apache, port: 80 }
  - { role: postgres_client, 
      dbname: blarg, 
      other_parameter: 12 }

where roles/postgres_client/meta/main.yml might include something like

  - { role: postgres_common }
  - { role: stunnel, 
      client: yes,
      local_port: 5432
      remote_host: db_host
      remote_port: 15432 

Such nested dependencies can get messy to maintain when the number of roles in an orchestration grows. I therefore wonder if anyone has found an easy way to generate a graph of such dependencies, either graphically (dot or neato?) or just as an indented text graph? Such a tool could help reduce the maintenance complexity.


The following python script worked for me:

#!/usr/bin/env python

import sys
import gv
from glob import glob
import yaml

g = gv.digraph('roles')

role_nodes = {}

def add_role(role):
    if role not in role_nodes:
        role_nodes[role] = gv.node(g, role)

def link_roles(dependent, depended):

for path in glob('roles/*/meta/main.yml'):
    dependent_role = path.split('/')[1]


    with open(path, 'r') as f:
        for dependency in yaml.load(['dependencies']:
            depended_role = dependency['role']

            link_roles(dependent_role, depended_role)

gv.layout(g, 'dot')
gv.render(g, 'png', 'doc/ansible-roles.png')

You can use Graphviz to do this like the ginas project has.

Need Your Help

NameError: global name 'messagebox' is not defined - CONSOLE MODE

python tkinter messagebox

I've imported tkinter.messagebox, and my program runs smoothly when I F5 and test it on IDLE.

Does fhir has a resource to represent a workflow task?


We are trying to integrate fhir in our product.