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

512 views
Skip to first unread message

russell choudhury

unread,
Jul 25, 2017, 10:12:07 AM7/25/17
to Ansible Project
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 <module>
    def main():
  File "/tmp/ansible_fDi5pe/ansible_module_helloworld.py", line
 5, in <module>
    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 <module>\n    def main():\n  File \"/tmp/ansible_fDi5pe/ans
ible_module_helloworld.py\", line 5, in <module>\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)<module>()\n-> def main():\n(Pdb) *** NameError: na
me 'false' is not defined\n(Pdb) \n",
    "msg": "MODULE FAILURE",
    "rc": 0
}

======================================================
Ansible playbook (helloworld.yml)

---
- hosts: localhost
  tasks:
    - name: Test that my module works
      helloworld: 
      register: result

    - debug: var=result  
=====================================
Ansible module (helloworld.py)

#!/usr/bin/python

from ansible.module_utils.basic import *

def main():
    import pdb; pdb.set_trace()  
    module = AnsibleModule(argument_spec={})
    response = {"hello": "world"}
    module.exit_json(changed=False, meta=response)


if __name__ == '__main__':  
    main()

=====================================
I would like to go into interactive mode so I can debug a module I am creating
Please note that this module works completely fine when I remove the pdb line

pdb is also working find when I don't user ansible

thanks for the help




Martin Krizek

unread,
Jul 25, 2017, 10:19:00 AM7/25/17
to ansible...@googlegroups.com
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
> --
> 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 post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/8114a613-d9da-46d4-9a64-6e49c0296dae%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

russell choudhury

unread,
Jul 25, 2017, 10:39:08 AM7/25/17
to Ansible Project
thanks soo much for the fast reply... it worked :D

'import epdb; epdb.set_trace()' seems to work aswell
Reply all
Reply to author
Forward
0 new messages