ansible-playbook-grapher icon indicating copy to clipboard operation
ansible-playbook-grapher copied to clipboard

[Feature request] Multiple playbooks in one graph

Open jheidbrink opened this issue 2 years ago • 6 comments

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

jheidbrink avatar Jun 15 '22 11:06 jheidbrink

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

haidaraM avatar Jun 17 '22 22:06 haidaraM

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.

Screenshot 2022-06-18 at 00 57 09

When using --include-role-tasks, I need to find a way to not render the same tasks multiple times like in the following screenshots: Screenshot 2022-06-18 at 01 00 33

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

haidaraM avatar Jun 17 '22 23:06 haidaraM

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.

jheidbrink avatar Jun 19 '22 16:06 jheidbrink

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.

jheidbrink avatar Jun 19 '22 16:06 jheidbrink

Can you give a try to #111? I made some changes to

haidaraM avatar Jun 21 '22 23:06 haidaraM

I created some simple example playbooks to test this, and the results look good!

jheidbrink avatar Jun 22 '22 07:06 jheidbrink