I wrote two callback-plugins which use an identical log-method, so I would like to share this code. Doing so, I moved that code to an external file which I would like to import.
Currently, I always get an error, that this external file can not be found. Any help would be apricated.
My structure does look like this
workspace |- playbooks |- ansible.cfg |- my.yml |- playbooks.yml |- collections |- plugins |- action |- callback |- general |- log_errors.py |- log_hosts.py |- filter |- lookup |- roles |- my |- roles |- ...
For using this structure, the ansible.cfg in the playbooks-dir has be modified:
roles_path = ../roles collections_paths = ../collections action_plugins = ../plugins/action callback_plugins = ../plugins/callback lookup_plugins = ../plugins/lookup filter_plugins = ../plugins/filter
The above works fine, also the custom callback-plugins work if I don’t share the code.
For using the shared code I did the following changes.
Added the file containing the shared code into a new directory:
workspace |- module_utils |- logging.py
Updated the path in the ansible.cfg:
module_utils = ../module_utils
In my callback-plugins I added the following line:
from ansible.module_utils.logging import write_log
If I execute a playbook, I get the following warning:
[WARNING]: Skipping plugin (<some_path>/plugins/callback/general/log_errors.py) as it seems to be
invalid: No module named ‘ansible.module_utils.logging’
If however, I copy the file from my customer folder module_utils to /usr/local/lib/python3.8/dist-packages/ansible/module_utils/logging.py it works.
Any Ideas what I am doing wrong?
Update: Also renaming my custom module_utils-folder to something else (while updating path in ansible-cfg and import) did not do the trick.