# Use plugin inventory ini in module

**URL:** <https://forum.ansible.com/t/use-plugin-inventory-ini-in-module/15381>\
**Category:** Ansible Developer\
**Tags:** azure\
**Created:** [May 31, 2014, 6:57am UTC](https://forum.ansible.com/t/use-plugin-inventory-ini-in-module/15381 "2014-05-31T06:57:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Johren](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@Martin\_Johren](https://forum.ansible.com/u/Martin_Johren)\
**Post date:** [May 31, 2014, 6:57am UTC](https://forum.ansible.com/t/use-plugin-inventory-ini-in-module/15381/1 "2014-05-31T06:57:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 31, 2014, 2:54pm UTC](https://forum.ansible.com/t/use-plugin-inventory-ini-in-module/15381/2 "2014-05-31T14:54:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Martin\_Johren](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@Martin\_Johren](https://forum.ansible.com/u/Martin_Johren)\
**Post date:** [June 1, 2014, 6:12pm UTC](https://forum.ansible.com/t/use-plugin-inventory-ini-in-module/15381/3 "2014-06-01T18:12:21Z")

</div>

thanks for the info. seems reasonable.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 6, 2014, 12:00pm UTC](https://forum.ansible.com/t/use-plugin-inventory-ini-in-module/15381/4 "2014-06-06T12:00:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Martin\_Johren](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@Martin\_Johren](https://forum.ansible.com/u/Martin_Johren)\
**Post date:** [June 6, 2014, 3:21pm UTC](https://forum.ansible.com/t/use-plugin-inventory-ini-in-module/15381/5 "2014-06-06T15:21:20Z")

</div>

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?
