¿Hay una manera fácil de generar un gráfico de dependencias de roles de Ansible?

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 }

donde 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.

preguntado el 12 de febrero de 14 a las 08:02

3 Respuestas

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(f.read())['dependencies']:
            depended_role = dependency['role']

            link_roles(dependent_role, depended_role)

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

Respondido el 24 de junio de 16 a las 08:06

This answer could be improved by explaining cómo it addresses the problem stated in the question. - user439793

Running: "ImportError: No module named gv". pip install gv: "No distributions at all found for gv" - Oso travis

@TravisBear The gv module is part of Graphviz, it is not distributed on PyPI. You can install it using the package manager of your linux distribution (apt, yum...). Package name may be something like graphviz-python or python-graphviz. - sebn

He hecho un versión alternativa usar Graphviz python package - El búho

Puedes usar Graphviz to do this like the ginas project tiene.

contestado el 16 de mayo de 14 a las 15:05

# install gv on ubuntu
sudo apt-get install python3-gv

Respondido 04 Feb 21, 14:02

This answer could be improved by showing how python-gv answers OP's problem. - zkoza

I think this should actually just be added to sebn's answer. It seems to be the missing piece for most of those who don't get how to install gv. - Martín Thorsen Ranang

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.