I have an Ansible collection for managing TrueNAS boxes. The core is a utility class that talks to the middleware daemon to check the configuration and make changes. There's a command-line utility to talk to the daemon, but there's also a Python module. I'd like to allow the Ansible user to choose which approach to use, just as they can choose how to ssh to a remote machine, or how to become root. In other words, somewhere in my plugin code I'll have
if method_to_use == 'First':
method = FirstMethod()
else:
method = SecondMethod()
But how do I set method_to_use
in a playbook, then retrieve it in a plugin that's been called on the remote machine?
My understanding is that inventory variables aren't available inside plugins. But since all of my modules are part of a collection, maybe there's something like a collection-level variable or fact that could be set?
Obviously, I could just have a method: {first|second}
option for each module, but I'd prefer not to make the user add that to every single play in their playbook.
I've seen one approach where the first play on any client is something like:
tasks:
- set_config:
method: first
where set_config
is a plugin that takes its method
option and stashes it in a file on the client. Then later plugins read that file to get the configuration. But that seems like a hack. I'm wondering whether there might be a more elegant solution.