How to debug custom Ansible module in Python?

Viewed 115

We use Ansible playbooks that call custom Python modules running on AWX/Centos. I need to make some changes to the modules and want to run them locally in my dev environment which is windows. I want the ability to step through the code & not run things remotely (WSL is fine though).

I can't run the module directly in Windows since pip install Ansible and pip install AnsibleRunner both fail (which I think is expected).

I was able to launch my playbook using AnsibleRunner in WSL but it hung (I think it was trying to connect to localhost via ssh). Even if it ran the playbook/module, I suspect it wouldn't stop on my breakpoints. Now I am running my module directly (ex module.run_module()) in wsl and I can step into it but it never returns after calling the following:

module = AnsibleModule(
    argument_spec=module_args,
    supports_check_mode=True
)

Any ideas why it would stop here? No logs or errors are generated. Any other recommendations? I want to be able to make code changes and test/step through them easily. I've done remote debugging with other technologies and it sucks so I want to avoid it.

1 Answers

I figured it out, it was blocking because AnsibleModule was trying to populate _ANSIBLE_ARGS by reading from stdin. By inspecting the code, I realized it will use a file instead if the first arg is a valid file.

So I'm now able to step through my custom module called from a test program running in VSCode on Windows via WSL/Ubuntu.

Related