How to debug/test ansible module. Python interactive mode (pdb)

Hi, I want to be able to go into interactive mode while creating a module. To do this I am adding the following line to my python code:

import pdb; pdb.set_trace()

when I run my ansible playbook with the following line:
ansible-playbook -i hosts helloworld.yml -vvv

I get this error:

The full traceback is:
Traceback (most recent call last):
File “/tmp/ansible_fDi5pe/ansible_module_helloworld.py”, line
5, in
def main():
File “/tmp/ansible_fDi5pe/ansible_module_helloworld.py”, line
5, in
def main():
File “/usr/lib/python2.7/bdb.py”, line 49, in trace_dispatch
return self.dispatch_line(frame)
File “/usr/lib/python2.7/bdb.py”, line 68, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit

fatal: [localhost]: FAILED! => {
“changed”: false,
“failed”: true,
“module_stderr”: “Traceback (most recent call last):\n Fil
e "/tmp/ansible_fDi5pe/ansible_module_helloworld.py", line 5,
in \n def main():\n File "/tmp/ansible_fDi5pe/ans
ible_module_helloworld.py", line 5, in \n def main(
):\n File "/usr/lib/python2.7/bdb.py", line 49, in trace_dis
patch\n return self.dispatch_line(frame)\n File "/usr/lib/
python2.7/bdb.py", line 68, in dispatch_line\n if self.quit
ting: raise BdbQuit\nbdb.BdbQuit\n”,
“module_stdout”: “> /tmp/ansible_fDi5pe/ansible_module_hell
oworld.py(5)()\n → def main():\n(Pdb) *** NameError: na
me ‘false’ is not defined\n(Pdb) \n”,
“msg”: “MODULE FAILURE”,
“rc”: 0
}

Hi,

Using epdb instead of pdb should do the trick, so "import epdb;
epdb.serve()" and then you connect via "epdb.connect()" from a python
shell. See https://pypi.python.org/pypi/epdb/.

Martin

thanks soo much for the fast reply… it worked :smiley:

‘import epdb; epdb.set_trace()’ seems to work aswell