ansible-playbook-grapher
ansible-playbook-grapher copied to clipboard
[Feature request] Multiple playbooks in one graph
Is your feature request related to a problem? Please describe.
I have a folder with playbooks and roles. Some of the roles are referenced in multiple playbooks. I would like to generate a graph with all playbooks and referenced roles. When two playbooks reference the same role, I want to have only one node for that role, and edges from both playbook nodes that reference it to that node.
Describe alternatives you've considered
for playbook in playbooks/*.yaml
do
ansible-playbook-grapher --save-dot-file $playbook
done
And then manually merge the files. You can use grep roles *.dot | sed -e 's@.*playbooks/roles/\(\S\+\)".*@\1@' | sort | uniq --repeated
to extract a list of roles that are actually referenced in multiple playbooks.
Additional context In case that's interesting, these are the playbooks/roles that I want to graph: https://github.com/magma/magma/tree/master/experimental/cloudstrapper
When two playbooks reference the same role, I want to have only one node for that role, and edges from both playbook nodes that reference it to that node.
I just realized this is not the case even with one playbook using the same role multiple times (in various plays, for example). I should fix that. It should not be different for various playbooks anyway.
And then manually merge the files. You can use grep roles *.dot | sed -e 's@.playbooks/roles/(\S+)".@\1@' | sort | uniq --repeated to extract a list of roles that are actually referenced in multiple playbooks.
If I understand correctly, your main goal is to identify the roles referenced in multiple playbooks?
Making the grapher takes multiple playbooks as arguments and parsing them into a single graph can be tricky to implement given the way the grapher is designed around a single playbook file. A workaround to have multiple playbooks in the same graph is to use import_playbook
that is supported by the grapher. You can do something like this:
---
- name: Import playbook
import_playbook: example.yml
- name: Import playbook 2
import_playbook: nested_include_tasks.yml
# ....
This is an overview of when multiple plays use the same roles (this is a quick test I did). It should be easy to implement by replacing the node generated id by the role name.
When using --include-role-tasks
, I need to find a way to not render the same tasks multiple times like in the following screenshots:
And here is the playbook:
---
- hosts: all
pre_tasks:
- name: Pretask
debug: msg="Pretask"
- name: Pretask 2
debug: msg="Pretask"
tags:
- pre_task_tags
post_tasks:
- name: Posttask
debug: msg="Postask"
- name: Posttask 2
debug: msg="Postask"
roles:
- role: fake_role
when: ansible_distribution == "Debian"
tags:
- role_tag
- role: display_some_facts
tasks:
- name: Add backport {{backport}}
apt_repository:
filename: "{{backport}}"
state: present
- name: Install packages
apt:
name: "{{packages}}"
state: latest
- hosts: database
roles:
- role: fake_role
when: ansible_distribution == "Debian"
tags:
- role_tag
- role: display_some_facts
- hosts: server
roles:
- role: display_some_facts
If I understand correctly, your main goal is to identify the roles referenced in multiple playbooks?
Not my main goal, just an intermediate step that could help when trying manually merging the dotfiles.
A workaround to have multiple playbooks in the same graph is to use import_playbook that is supported by the grapher.
Thanks, that sounds good! When the tests in pull/111 are green, I'll give this a try.
Can you give a try to #111? I made some changes to
I created some simple example playbooks to test this, and the results look good!