# How to pass inventory information to modules

**URL:** <https://forum.ansible.com/t/how-to-pass-inventory-information-to-modules/21025>\
**Category:** Ansible Project\
**Created:** [March 30, 2016, 10:13pm UTC](https://forum.ansible.com/t/how-to-pass-inventory-information-to-modules/21025 "2016-03-30T22:13:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miroslav\_Hudec](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@Miroslav\_Hudec](https://forum.ansible.com/u/Miroslav_Hudec)\
**Post date:** [March 30, 2016, 10:13pm UTC](https://forum.ansible.com/t/how-to-pass-inventory-information-to-modules/21025/1 "2016-03-30T22:13:14Z")

</div>

Hello everyone,

as a part of my Bachelor thesis I’m trying to make few modules for ansible, which would be used to configure MikroTik devices using API. I’m currently having troubles finding a way to “tell” the module things like hostname, username and password. I have these stored in hosts file, so I can use these as variables in playbook (since the connection type is local). Unfortunately, that makes the playbook look way too complicated, since every task has the exact same parts: hostname, username and password (and after that module-specific options). From what I’ve read, Ansible doesn’t allow modules to see these variables directly because of some security concerns, or, whatever.

That’s why I decided to make a piece of code that would go trough the hosts file and read usernames etc. for each host. But at this point I’ve realized that I don’t know how to tell that module the name of the host to look for. When the task will run for the host named “Router2”, is there a way to tell that module: Hey, you’re now working for Router2, try to find it’s username etc… Or is there an entire different approach? Thank you for any suggestions.

One last thing I’d like to know… Is there a way to use multi-file modules, but store the shared files in different location than module\_utils?

I want to say that I’m new to Ansible so please bear with me. I appreciate any suggestions. Thank you.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 31, 2016, 2:18am UTC](https://forum.ansible.com/t/how-to-pass-inventory-information-to-modules/21025/2 "2016-03-31T02:18:38Z")

</div>

> That's why I decided to make a piece of code that would go trough the  
> hosts file and read usernames etc. for each host. But at this point I've  
> realized that I don't know how to tell that module the name of the host to  
> look for. When the task will run for the host named "Router2", is there a  
> way to tell that module: Hey, you're now working for Router2, try to find  
> it's username etc... Or is there an entire different approach? Thank you  
> for any suggestions.

​I suggest either creating a connection plugin to deal with these issues or  
do like most cloud modules having a common file/environment vars that are  
used as a fallback for the plugin's options.​

> One last thing I'd like to know... Is there a way to use multi-file  
> modules, but store the shared files in different location than  
> module\_utils?

​Currently no way to do this, if you are going to use a shared lib you need  
it installed on the 'target' machine that will execute the module.​

---

<div class="post-metadata">

**Author:** ![James\_Martin](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@James\_Martin](https://forum.ansible.com/u/James_Martin)\
**Post date:** [March 31, 2016, 4:32am UTC](https://forum.ansible.com/t/how-to-pass-inventory-information-to-modules/21025/3 "2016-03-31T04:32:16Z")

</div>

Miroslav,

Here is one work around – design your inventory something like this:

[routers]  
router1 ansible\_connection=local router\_user=foo router\_password=bar  
router2 ansible\_connection=local router\_user=baz router\_password=biz  
router3 ansible\_connection=local router\_user=fax router\_password=fix

Here you are essentially creating an alias for each device, giving each one an inventory hostname, but the connection is still local.

in your playbook you could do something like this (of course I’ve made up these module options, as I’ve no idea what you’ve built):

- hosts: routers  
gather\_facts: no  
tasks:
- name: configure routers  
mikrotik:  
user: “{{ router\_user }}”  
password: “{{ router\_password }}”  
setting: foo

This will execute against all of the hosts you’ve defined in the inventory and slurp up the inventory variables you’ve defined, router\_user, router\_password.

- James
