How to debug ansible action plugin

I’d like to be able to debug a custom action plugin using VS Code. With a module, the module is created in the script and it calls exit_json, so modules are pretty easy to get working in a debugger, but how would I do the same thing with a custom ActionPlugin?

How could I instantiate an ActionPlugin instance from main() in a python script?

Thanks

The simplest option is to create a breakpoint in your code and under the Python debugger run ansible ... -m action_plugin. You can also run it through ansible-playbook if you need some setup tasks first before you call your action plugin.

With some more information you want something like the following launch configuration:

{
    "name": "Python Debugger: Module",
    "type": "debugpy",
    "request": "launch",
    "module": "ansible",
    "args": [
        "adhoc",
        "localhost",
        "-m",
        "my_action",
        "--playbook-dir",
        "${workspaceFolder}"
    ]
}

In this case I’m running the ansible (adhoc) command through the Python module option. I’ve also set --playbook-dir so it loaded my custom action plugin based on the normal playbook loading rules. You can ignore that if you are loading your action plugin in another way.

When running Ansible will run as normal but will top at your breakpoint when the action plugin is run

1 Like

Thanks, this seems to work great.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.