| 1 | <!-- |
| 2 | Copyright (c) 2022 Dell Inc., or its subsidiaries. All Rights Reserved. |
| 3 | |
| 4 | Licensed under the GPL, Version 3.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | https://www.gnu.org/licenses/gpl-3.0.txt |
| 9 | --> |
| 10 | # How to Perform Debugging |
| 11 | |
| 12 | The following steps enables you to debug OpenManage Ansible Modules from an IDE either from local Linux or from a remote debugger on Windows. |
| 13 | |
| 14 | 1. Install OpenManage Ansible Modules from Galaxy `ansible-galaxy collection install dellemc.openmanage`. On Ubuntu this defaults to `$HOME/.ansible/collections/ansible_collections/dellemc/openmanage/` The problem with this is that this location is not in PYTHONPATH which will cause problems with the debugger. |
| 15 | 2. To resolve python path issues, move the `openmanage ` collection to align with the rest of Dell's code which is in PYTHONPATH with `sudo mv $HOME/.ansible/collections/ansible_collections/dellemc/openmanage/ /usr/local/lib/python3.X/dist-packages/ansible_collections/dellemc/`. The path may be different on your system, but it should be placed with your other python packages. |
| 16 | Alternatively, you can add the directory `$HOME/.ansible/collections/ansible_collections/dellemc/openmanage/` to PYTHONPATH with `export PYTHONPATH=$PYTHONPATH:$HOME/.ansible/collections`. |
| 17 | 1. The location may be different for but the key is `openmanage` must be accessible within the `ansible_collections.dellemc` namespace. That is to say, the path should look like `<SOME_PREFIX (usually dist-packages)>/ansible_collections/dellemc/openmanage` |
| 18 | 2. Sometimes, `from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError` still reports an error. This error can be ignored safely. |
| 19 | 3. Some IDEs may require a restart in order to rescan the available packages for import statements to resolve. |
| 20 | 3. Create a file with any name. We will use `args.json`. Fill it with the arguments you wish to provide to the module: |
| 21 | |
| 22 | { |
| 23 | "ANSIBLE_MODULE_ARGS": { |
| 24 | "idrac_ip": "192.168.1.63", |
| 25 | "idrac_user": "root", |
| 26 | "idrac_password": "password", |
| 27 | "share_name": "some_share", |
| 28 | "share_user": "some_username", |
| 29 | "share_password": "some_password" |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | For more information about injecting arguments , see the Ansible [docs](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html#exercising-module-code-locally). |
| 34 | |
| 35 | 4. Execute one of the modules by running `python -m ansible.modules.<some_module> /tmp/args.json`. |
| 36 | |
| 37 | How to run this in an IDE is described below. |
| 38 | |
| 39 | ## PyCharm |
| 40 | |
| 41 | ### On Remote Windows |
| 42 | |
| 43 | The following steps helps to run and debug OpenManage Ansible Modules installed on the Linux VM using a remote debugger configured on Windows PyCharm IDE. |
| 44 | |
| 45 | 1. Download a copy of [the code](https://github.com/dell/dellemc-openmanage-ansible-modules) and open the folder with PyCharm. |
| 46 | 2. Go to File->Settings->Project:<name_of_your_project>->Python Interpreter |
| 47 | 3. Click the gear and then click add |
| 48 | 4. Use `SSH Interpreter` and then add the Linux box mentioned above or another remote target of your choice. |
| 49 | |
| 50 | ### On Local Linux |
| 51 | |
| 52 | 1. You will need to configure the IDE to use the `args.json` file you created above. In PyCharm do this by going to Run-Edit Configurations. In `Parameters` add `<ABSOLUTE_PATH>\args.json`. This will pass the JSON file as an argument to the module when it runs. You should now be able to debug the module directly. |
| 53 | 2. It is also possible to pass the arguments within the Python script itself by updating the `main` function with: |
| 54 | |
| 55 | basic._ANSIBLE_ARGS = to_bytes(json.dumps({'ANSIBLE_MODULE_ARGS': {"idrac_ip": "192.168.0.1", "idrac_user": "username", "idrac_password": "password"}})) |
| 56 | set_module_args(args) |
| 57 | |
| 58 | 3. If you would like to set PYTHONPATH with PyCharm you can do that by going to Run->Edit Configurations->Environment Variables and add 'PYTHONPATH=$PYTHONPATH:$HOME/.ansible/collections/'. |