Use plugin inventory ini in module

Hi,

I have a question regarding the ini files in the plugin inventory section:

Since I want to use the same config file to define a cloud username + api key for a module and an inventory plugin I would like to provide only one function which does the parsing and mapping stuff. So the question is: where to put that file so that this function is accessible from the plugin inventory script as well as from a module, e.g. in an azure module I need subscription_id and a path to a private key file. This is also needed when I want to provide an inventory plugin.
My suggestion is to put it in lib/ansible/module_utils? Or in lib/ansible/inventory? Or is it in general a bad idea?

thx

Martin

Typically those are handled via environment variables, which are used by both the inventory script and module (this is how the AWS/EC2 stuff works). The files in module_utils are typically part of the templated code that is sent on to host systems, so inventory scripts should definitely not be using code there. Unfortunately, there may have to be some duplication of code between those, but stuff like that is generally pretty simple and only needs to be written once.

thanks for the info. seems reasonable.

Possibly if not trying to add something for core you could just import a python library in both places.

We have discussed making a lib/ansible/utils/inventory/ directory for common code that might be used by inventory plugins, to provide some common functions across all inventories.

I must disagree with James here:

"module_utils are typically part of the templated code that is sent on to host systems, so inventory scripts should definitely not be using code there. "

Anything in the module_utils package is actually importable by other python programs locally so it’s technically possible to put stuff there and share it between places.

michael, thanks for your answer.
then i think it might make sense to put code into this place to read for example credential data from a config file or an environment variable.
a next question then comes also to my mind: what about data structures? i mean when you have a module for a cloud provider they will return some json or xml or whatever data structure with information about a running vm, etc.
since it could be a good practice to abstract that in the module because you don’t want to pass around the raw data of the responses and tie the user playbooks to the api version of the cloud service, it would also be useful to have a place for that data structures to use it in the module as well as in the inventory file. does that make sense? what do you think?