haidaraM / ansible-playbook-grapher

A command line tool to create a graph representing your Ansible playbook tasks and roles
GNU General Public License v3.0
640 stars 53 forks source link

Generate graphs for galaxy/globally installed playbooks #203

Open j2udev opened 3 weeks ago

j2udev commented 3 weeks ago

Is your feature request related to a problem? Please describe. I'd like to be able to generate a graph by referencing the globally/galaxy installed playbook instead of the file location. So instead of

ansible-playbook-grapher /path/to/my/playbook.yml

I'd like to be able to do:

ansible-playbook-grapher my_namespace.my_collection.my_playbook

Describe the solution you'd like If I can run my playbook like this:

ansible-playbook my_namespace.my_collection.my_playbook

then I should also be able to

ansible-playbook-grapher my_namespace.my_collection.my_playbook

Describe alternatives you've considered N/A

Additional context N/A - ty for building this tool :)

haidaraM commented 3 weeks ago

Hi,

This should be doable. I see what changes have to be done. Do you have some examples of collection on Ansible Galaxy or somewhere else containing playbooks I can use as examples for testing?

ty for building this tool :)

Welcome :-)

j2udev commented 2 weeks ago

Apologies for the delay. I created a super minimal collection and threw it up on Ansible Galaxy. You can install it and run it with:

ansible-galaxy collection install j2udev.test_collection
(inspect the contents of the playbook if you're concerned about malicious intent... or you can take my word that it's just a simple debug task haha)
ansible-playbook j2udev.test_collection.test

If the collection needs to be a little more complicated just let me know... it only has that single playbook atm.

haidaraM commented 1 week ago

No worries, thanks. If you can make it a bit more complicated, that would be cool as well :-)

j2udev commented 1 week ago

Alright I added two roles to the collection that the playbook calls. You can reinstall the collection with:

ansible-galaxy collection install j2udev.test_collection --force
haidaraM commented 1 week ago

Perfect :-)

haidaraM commented 6 days ago

Can you give a try to:

pip install "ansible-playbook-grapher @ git+https://github.com/haidaraM/ansible-playbook-grapher@allow-reading-playbooks-in-collection"
ansible-playbook-grapher --include-role-tasks j2udev.test_collection.test

Testing some stuff at the moment.

j2udev commented 6 days ago

Looks good, this is great :) One small comment, it might be nice to have an option to render the playbook node as j2udev.test_collection.test instead of it listing the filepath. It would also give you a little more screen real estate.

j2udev commented 6 days ago

One other thing I notice and perhaps I'm just doing something wrong... but in another collection I'm using a non-built-in module (specifically ansible.posix.authorized_key) which appears to throw an error.

  File "/home/wardjl/.local/share/pipx/venvs/ansible-playbook-grapher/lib/python3.12/site-packages/ansible/parsing/mod_args.py", line 349, in parse
    raise AnsibleParserError("couldn't resolve module/action '{0}'. This often indicates a "
ansible.errors.AnsibleParserError: couldn't resolve module/action 'ansible.posix.authorized_key'. This often indicates a misspelling, missing collection, or incorrect module path.

I can, however, run any playbook that calls this role just fine.

haidaraM commented 6 days ago

One small comment, it might be nice to have an option to render the playbook node as j2udev.test_collection.test instead of it listing the filepath.

Will do. Good idea!

but in another collection I'm using a non-built-in module (specifically ansible.posix.authorized_key) which appears to throw an error.

I guess you see ansible.posix when you run ansible-galaxy collection list? If yes, where it is stored? Is it a non default location? Checking if I can reproduce this with a module used in a collection.

j2udev commented 6 days ago

One small comment, it might be nice to have an option to render the playbook node as j2udev.test_collection.test instead of it listing the filepath.

Will do. Good idea!

but in another collection I'm using a non-built-in module (specifically ansible.posix.authorized_key) which appears to throw an error.

I guess you see ansible.posix when you run ansible-galaxy collection list? If yes, where it is stored? Is it a non default location? Checking if I can reproduce this with a module used in a collection.

# /usr/lib/python3/dist-packages/ansible_collections
Collection                               Version
---------------------------------------- -------
amazon.aws                               7.6.1  
ansible.netcommon                        5.3.0  
ansible.posix                            1.5.4
haidaraM commented 6 days ago

Ok and what does ansible --version give you? Looking for the different paths and locations in the output.

j2udev commented 6 days ago

Ok and what does ansible --version give you? Looking for the different paths and locations in the output.

ansible [core 2.16.9]
  configured module search path = ['/home/wardjl/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/wardjl/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
haidaraM commented 6 days ago

it might be nice to have an option to render the playbook node as j2udev.test_collection.test instead of it listing the filepath. It would also give you a little more screen real estate.

This should be fixed in the last version.

However, I'm not able to reproduce the error you are seeing. From this thread, /usr/lib/python3/dist-packages/ansible_collections is an old location and it seems like the grapher is not considering it somehow.

To validate my assumption, can you do a fresh install of Ansible and collections on a machine via pip and try to run the grapher on the same playbook? Or install the collections in ~/.ansible/collections/ansible_collections?

Here is what I have when running ansible-galaxy collection list:

# /Users/mohamedelmouctarhaidara/.ansible/collections/ansible_collections
Collection                               Version
---------------------------------------- -------
ansible.posix                            1.5.4
community.docker                         3.8.1
community.general                        10.0.1
haidaram.test_collection                 1.0.0
j2udev.test_collection                   1.0.1

# /Users/mohamedelmouctarhaidara/projects/ansible-playbook-grapher/venv/lib/python3.11/site-packages/ansible_collections
Collection                               Version
---------------------------------------- -------
amazon.aws                               7.3.0
ansible.netcommon                        5.3.0
ansible.posix                            1.5.4
# ....

And ansible --version:

ansible [core 2.17.2]
  config file = None
  configured module search path = ['/Users/mohamedelmouctarhaidara/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/mohamedelmouctarhaidara/projects/ansible-playbook-grapher/venv/lib/python3.11/site-packages/ansible
  ansible collection location = /Users/mohamedelmouctarhaidara/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/mohamedelmouctarhaidara/projects/ansible-playbook-grapher/venv/bin/ansible
  python version = 3.11.3 (main, May 11 2023, 21:06:27) [Clang 14.0.3 (clang-1403.0.22.14.1)] (/Users/mohamedelmouctarhaidara/projects/ansible-playbook-grapher/venv/bin/python)
  jinja version = 3.1.4
  libyaml = True
j2udev commented 5 days ago

I updated my ansible.cfg collections_paths with the path reported by ansible-galaxy collection list and it started working :) I also pulled your changes and the shorthand for the playbook node is much nicer! Tyvm! Feel free to close this!

haidaraM commented 5 days ago

Perfect! Will close this once released