Access reusable code & files from within custom lookup plugin ?

Hello Ansible community,

I am developing a series of custom plugins, with common Python code and common json schema files. Take for example a lookup plugin that needs to import the external Python code and open the json schema files. In a unit test this all works. But in a playbook these files can not be found.

This is of course because the plugin code runs on a different environment. Usually this code should be put into a directory named module_utils. For a lookup plugin apparently this doesn’t work.

Is there a way to make this work for (lookup) plugins ?

THX

How does this 'not work?' code in module_utils is used on controller
side plugins all the time, though it is primarily intended for
modules.

Let me rephrase it: I’ve got playbooks that should access locally defined lookup plugins. Locally as in “not in a collection or role”. Those lookup plugins should be able to import common code and read common resource files. The lookup plugin should be put into the directory “/lookup_plugins”. That is as far as I got. I’ve tried various directories & methods to access the common code and resource files. That does ‘not work’. So, the bottom line:

  1. In what directory should I put the common code ?

  2. How to import the common code from within the lookup plugin ?

  3. In what directory should I put the resource files ?

  4. How to I open the resource files from within the lookup plugin ? (or in the common code)

thx

This might help:

Plugin writing guide:
https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html

example:
https://gist.github.com/tgerla/bbfa956e5463361447c6

1. In what directory should I put the common code ?

module_utils (this works fine in collections and as adjacent dir in roles/plays)

2. How to import the common code from within the lookup plugin ?

same as modules do so ansible.module_utils.<name or rest of path>

3. In what directory should I put the resource files ?

not sure what these are but normally files/ in roles or adjacent to
play (there is no facility in collections)

4. How to I open the resource files from within the lookup plugin ? (or in the common code)

if using #3, they should already be in the expected search paths for
lookups as long as you use the existing "dwim" functions, most notably
the `self.find_file_in_search_path` available to all lookups.