This may be just something that is simple that I’m overlooking.
I am developing a set of action plugins for working with our salesforce instance. I’m relatively new to module/plugin development.
There is a well defined standard for doing argument validation in a module with a defined argument spec. It even handles callbacks to validate via checking an environment variable.
I’ve seen some examples, and looked through several bits of the core code, and it seems like the argument validation is handled differently for each.
I want to make the code so there is a possibility that I might be able to contribute it back to the community at some point. I am wrestling with the idea of handling it with some code in module_utils so it works for the entire collection, but before I go down that road, I want to make sure that I’m not missing something obvious.
Depending on which ansible-core versions you support, you will find that ActionBase has helper functions for this, like validate_argument_spec(). This was added in Action Plugin argspec validation by sivel · Pull Request #77013 · ansible/ansible · GitHub, which got into ansible-core 2.13.0 apparently. Right now a few action plugins in ansible-core use it (pause, debug, script, async_status, and validate_argument_spec according to a quick grep); none of the collections I have checked out locally use it so far.
To find out whether a diff is wanted, you can check self._play_context.diff. That’s equivalent to module._diff for modules. Besides that you simply have to return the diff key in the return values, same as for modules.
Something you have to watch out for is no_log, since you have to handle this yourself for action plugins: if you have no_log=True in your argument spec, you have to take care yourself to censor that value when outputting it somewhere.
Also note that ansible-test’s sanity checks will not compare the argument spec to the documentation to see whether there are differences.
I eventually found the value in self._task._diff eventually, but I know there can be ill effects if I’m not reading it from the right place. So I’ll get that switched aroundI’m pleased to say that the diff mode is beautiful though.
I’m glad you mentioned the no_log bit, because there is definitely some things for that.
I’m going to work on figuring out testing today. It seems there is a myriad of tools for testing, but I will start with ansible-test.
I’m not 100% sure it is really the right place, but at least it did work fine for me so far Maybe someone from @Core can comment on this? Would be nice to know what the right place to get this (and similar values) from is.
play_context should not be used by action plugins when task has the same property, it is a ‘temporary’ copy for when tasks are not yet templated, which should not be the case by the time the action plugin is loaded.
If you need to find the source for the different properties assigned to module, like diff, see this part of the code: