How to debug a community ansible module ?

41 views
Skip to first unread message

cmou...@redhat.com

unread,
Jul 22, 2020, 8:40:22 AM7/22/20
to Ansible Project
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


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 "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ModuleNotFoundError: No module named 'ansible_collections'

Best

Charles



Abhijeet Kasurde

unread,
Jul 22, 2020, 8:47:51 AM7/22/20
to ansible...@googlegroups.com
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 <rest of command>

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/386a706d-859f-429b-b100-d3d88ce1d3e8n%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Charles Moulliard

unread,
Jul 22, 2020, 9:45:29 AM7/22/20
to ansible...@googlegroups.com
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 <module>
    from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule

ModuleNotFoundError: No module named 'ansible_collections'
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/SlDn22N-1zM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFwWkHoGSb4T93_UefwDXBKunB_LUzs4TyTjf8pwhLE1H7cjZA%40mail.gmail.com.

Matt Martz

unread,
Jul 22, 2020, 9:51:04 AM7/22/20
to ansible...@googlegroups.com
`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.


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



--
Matt Martz
@sivel
sivel.net

Charles Moulliard

unread,
Jul 22, 2020, 9:58:31 AM7/22/20
to ansible...@googlegroups.com
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" ...

Reply all
Reply to author
Forward
0 new messages