I think I solved these two questions, thanks to Brian's comment on symlinks and general guidance by Matt. Appreciate.
TLDR: I wanted to have an ansible collection dir to be located anywhere, not following a particular dir structure, and satisfy the following requirements: 1. ansible-playbook should load the collection that is under development without doing galaxy-install. Similar to `pip install -e` mode. 2. Internal Import paths in my collection should use the full paths and resolve by the IDE. To do that, I did the following: 1. create an ansible skeleton dir at ~/somedir/collection 2. create a temp dir that satisfies the collection's path structure: mkdir -p /tmp/somedir 3. create a symlink to the path where you develop the collection: ln -s "$(pwd)" /tmp/somedir/ansible_collections/<namespace>/<collection_name> 3. Put in ansible.cfg the following [defaults] collections_paths = /tmp/somedir 4. this will make ansible to use the collection without requiring an installation step 5. To make python import paths resolve, I created an .env file in the collection's root dir stating PYTHONPATH=/tmp/somedir 6. This will make the python interpreter pick up import paths like: from ansible_collections.<namespace>.<coll_name> On Wed, Feb 22, 2023 at 10:10 PM Roman Dodin <dodin.ro...@gmail.com> wrote: > Yes, this is clear, but then it will point to the "installed" collection. > Unless developing directly in the ~/.ansible/collections or using symlinks > like Brian mentioned above, this won't use the files from my dev directory. > > I was looking for a way to make my local files in a dir that doesn't > belong to ~/.ansible/collections or any other dir structure that I can use > in a collections_path appear in dir that will resolve. > > Will move my dev dir to a predefined structure otherwise. > > On Wed, Feb 22, 2023 at 10:04 PM Matt Martz <m...@sivel.net> wrote: > >> As mentioned, you need to set `PYTHONPATH` in your IDE to the directory >> containing `ansible_collections`. Like: >> `PYTHONPATH=~/.ansible/collections`. >> >> I don't use a fancy IDE, so I don't have any direct guidance on where you >> would need to set this. Maybe the IDE doesn't respect this environment >> variable, or has other ways to configure it. >> >> Effectively, the `ansible_collections/` directory is similar to a Python >> namespace package, and the `collections/` directory is similar to >> `site-packages` used by python. So you just need to point `PYTHONPATH` or >> whatever else your IDE may use to all collections paths that ansible is >> configured to consult. I mentioned this in my original reply. >> >> On Wed, Feb 22, 2023 at 2:59 PM Roman Dodin <dodin.ro...@gmail.com> >> wrote: >> >>> As for the 2nd question the only workaround I found is to use relative >>> imports, which I want to get rid of. >>> Let me give you an example. In my collection dir I have the plugins >>> subdir that has: >>> >>> plugins >>> ├── httpapi >>> │ └── sr.py >>> ├── module_utils >>> │ └── sr.py >>> └── modules >>> └── get.py >>> >>> In my get.py I have to import a class from module_utils, and the full >>> import path looks like >>> >>> from >>> ansible_collections.<namespace>.<collection>.plugins.module_utils.srlinux >>> import MyClass >>> >>> How do I make my python instance to resolve this path? I understand that >>> at runtime ansible does add collections dir to the PYTHONPATH (or something >>> of sorts), but without running ansible, how do i make my dev environment to >>> resolve this path? >>> >>> Thank you all for your help and valuable inputs. >>> >>> PS. I can solve this with a relative path like >>> >>> from ..module_utils.sr import MyClass >>> >>> but I would like to not use relative imports if possible. >>> >>> >>> On Wed, Feb 22, 2023 at 9:48 PM Brian Coca <bc...@redhat.com> wrote: >>> >>>> ls -l ~/.ansible/collections/ansible_collections/ >>>> total 20 >>>> lrwxrwxrwx 1 bcoca bcoca 54 Oct 7 2021 bcoca -> >>>> /home/bcoca/work/collections/ansible_collections/bcoca >>>> >>>> On Wed, Feb 22, 2023 at 3:45 PM Brian Coca <bc...@redhat.com> wrote: >>>> > >>>> > symlinks do work, but you still need an ansible_collections/ dir >>>> > before your collection >>>> > >>>> > >>>> > >>>> > >>>> > -- >>>> > ---------- >>>> > Brian Coca >>>> >>>> >>>> >>>> -- >>>> ---------- >>>> Brian Coca >>>> >>>> >> >> -- >> Matt Martz >> @sivel >> sivel.net >> > -- You received this message because you are subscribed to the Google Groups "Ansible Development" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-devel+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/CAFBEvLsHYVbJF1u%2Bf1f0%3Du1TJetqrc5FzpK6mO-c%3DD0WVqA9Ww%40mail.gmail.com.