https://github.com/kb-perbyte/debuggingnetworkresourcemodule
Debugging Network Resource Modules for Ansible
https://github.com/kb-perbyte/debuggingnetworkresourcemodule
Last synced: 23 days ago
JSON representation
Debugging Network Resource Modules for Ansible
- Host: GitHub
- URL: https://github.com/kb-perbyte/debuggingnetworkresourcemodule
- Owner: KB-perByte
- Created: 2021-08-09T03:23:48.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-05-31T16:48:21.000Z (almost 3 years ago)
- Last Synced: 2025-08-08T17:55:55.735Z (8 months ago)
- Size: 412 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Debugging Ansible Module with VS Code
Debugging Ansible modules with VS code as the most interactive way,
#### Getting the module:
After getting the module installed from ansible-galaxy,or git clone the collection with all necessary dependencies
```
ansible-galaxy collection install cisco.ios
```
Look for the path where the module sits and establish VS code at the same location.
```
┌[machine@machine]-(~/.a/c/a/c/ios)
└> pwd
~/.ansible/collections/ansible_collections/cisco/ios
┌[machine@machine]-(~/.a/c/a/c/ios)
└> code.
```
#### Configuring VS code:
_(Use the same setting with a different port for debugging multiple modules)_
Once we are in vs code with the specific module we wish to debug, either use the debug option followed by the settings sign tagged ‘Open launch.json’ to generate the launch configuration file

_Or_
Use the following commands to the root of the collection for generating the same
```
┌[machine@machine]-(~/.a/c/a/c/i)
└> mkdir .vscode
┌[machine@machine]-(~/.a/c/a/c/i/.vscode)
└> nano launch.json
```
Drop the below config in your launch.json
```
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 3000 ,
"host": "127.0.0.1",
"justMyCode": false
},
]
}
```
Get debugpy and install it within the scope of your environment.
`pip install debugpy`
To start debugging go to the intended module and put the following lines on top of it,
```
import debugpy
debugpy.listen(3000)
debugpy.wait_for_client()
```
Well, the last step
Your inventory file should contain the specified variable
```
[all:vars]
ansible_network_import_modules=True
```
Now right after running your playbook using ansible-playbook
```
[machine@machine]-(~/W/debug_example)
└> ansible-playbook debug_playbook.yml
```
Expect the execution to be stuck at the task execution level waiting for you to manually start the module and add it to port 3000 (or any configured port).
Navigating to the module where debugpy import was used press F5 to start debugging

At this point, the debugger should hit your break points when in process.
For debugging ansible-navigator check a cool doc [here]: https://github.com/shatakshiiii/debuggingNavigator/blob/main/README.md
Refer to the VS code debugging guide for more information-
https://code.visualstudio.com/docs/editor/debugging
#### Works with:
- older Network Modules
- newer Network Resource Modules
- execution with Ansible Navigator
## Happy Debugging !!!