Access reusable code & files from within custom lookup plugin ?

154 views
Skip to first unread message

Jan Goyvaerts

unread,
Jan 11, 2023, 5:57:45 AM1/11/23
to Ansible Project
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



Brian Coca

unread,
Jan 11, 2023, 11:12:57 AM1/11/23
to ansible...@googlegroups.com
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.

----------
Brian Coca

Jan Goyvaerts

unread,
Jan 12, 2023, 3:19:45 AM1/12/23
to Ansible Project
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

Josh

unread,
Jan 13, 2023, 1:20:58 AM1/13/23
to Ansible Project

Brian Coca

unread,
Jan 13, 2023, 10:10:44 AM1/13/23
to ansible...@googlegroups.com
> 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.


--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages