How to debug a community ansible module ?

Hi,

As the packages to be imported by a python file are prefixed as “ansible_collections.”, how can we then locally debug an Ansible Module

Example: https://github.com/ansible-collections/community.kubernetes/blob/main/plugins/modules/k8s_log.py

from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule

class KubernetesLogModule(KubernetesAnsibleModule):

def init(self, *args, **kwargs):
KubernetesAnsibleModule.init(self, *args,
supports_check_mode=True,
**kwargs)

Result

/usr/local/bin/python3.7 “/Users/cmoullia/Library/Application Support/JetBrains/IntelliJIdea2020.1/plugins/python/helpers/pydev/pydevd.py” --multiproc --qt-support=auto --client 127.0.0.1 --port 57135 --file /Users/cmoullia/code/ansible/community.kubernetes/plugins/modules/k8s_log.py
pydev debugger: process 68087 is connecting

Connected to pydev debugger (build 201.8538.31)
Traceback (most recent call last):
File “”, line 983, in _find_and_load
File “”, line 953, in _find_and_load_unlocked
File “”, line 219, in _call_with_frames_removed
ModuleNotFoundError: No module named ‘ansible_collections’

Best

Charles

Try adding PYTHONPATH

For me it is like PYTHONPATH=/Volumes/data/src/ansible/lib:/Volumes/data/src/ansible:/Volumes/data/src/collections /Volumes/data/venv3/bin/python

That fails if I try what you suggest as the project (I suppose) is no installed as a collection but git cloned

(.venv) ~/code/ansible/ansible_collections $ PYTHONPATH=/Users/cmoullia/code/ansible/ansible_collections python3 community/kubernetes/plugins/modules/k8s_log.py
Traceback (most recent call last):
File “community/kubernetes/plugins/modules/k8s_log.py”, line 131, in
from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule
ModuleNotFoundError: No module named ‘ansible_collections’

ansible_collections is the python module that the code will try to load, so you cannot contain that in your PYTHONPATH, you need to set your PYTHONPATH to the directory that contains the ansible_collections directory.

fwiw, I wouldn’t recommend using this as the primary means to test or debug a module.

Follow the docs outlined at https://docs.ansible.com/ansible/devel/dev_guide/debugging.html#debugging-ansiblemodule-based-modules

If you bypass the normal execution flow, you aren’t properly testing the module.

Sorry but I don’t see how I could debug the code with a breakpoint declared within my IDEA tool using this command “ansible localhost -m ping -a ‘data=debugging_session’ -vvv” …