# Access reusable code & files from within custom lookup plugin ?

**URL:** <https://forum.ansible.com/t/access-reusable-code-files-from-within-custom-lookup-plugin/37541>\
**Category:** Ansible Project\
**Created:** [January 11, 2023, 10:57am UTC](https://forum.ansible.com/t/access-reusable-code-files-from-within-custom-lookup-plugin/37541 "2023-01-11T10:57:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Goyvaerts](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jan\_Goyvaerts](https://forum.ansible.com/u/Jan_Goyvaerts)\
**Post date:** [January 11, 2023, 10:57am UTC](https://forum.ansible.com/t/access-reusable-code-files-from-within-custom-lookup-plugin/37541/1 "2023-01-11T10:57:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 11, 2023, 4:10pm UTC](https://forum.ansible.com/t/access-reusable-code-files-from-within-custom-lookup-plugin/37541/2 "2023-01-11T16:10:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jan\_Goyvaerts](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jan\_Goyvaerts](https://forum.ansible.com/u/Jan_Goyvaerts)\
**Post date:** [January 12, 2023, 8:19am UTC](https://forum.ansible.com/t/access-reusable-code-files-from-within-custom-lookup-plugin/37541/3 "2023-01-12T08:19:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josh](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Josh](https://forum.ansible.com/u/Josh)\
**Post date:** [January 13, 2023, 6:20am UTC](https://forum.ansible.com/t/access-reusable-code-files-from-within-custom-lookup-plugin/37541/4 "2023-01-13T06:20:58Z")

</div>

This might help:

Plugin writing guide:  
[https://docs.ansible.com/ansible/latest/dev\_guide/developing\_plugins.html](https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html)

example:  
[https://gist.github.com/tgerla/bbfa956e5463361447c6](https://gist.github.com/tgerla/bbfa956e5463361447c6)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 13, 2023, 3:08pm UTC](https://forum.ansible.com/t/access-reusable-code-files-from-within-custom-lookup-plugin/37541/5 "2023-01-13T15:08:38Z")

</div>

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