How to pass inventory information to modules

60 views
Skip to first unread message

Miroslav Hudec

unread,
Mar 30, 2016, 6:13:14 PM3/30/16
to Ansible Project
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.

Brian Coca

unread,
Mar 30, 2016, 10:18:45 PM3/30/16
to ansible...@googlegroups.com
On Wed, Mar 30, 2016 at 3:13 PM, Miroslav Hudec <mij...@gmail.com> wrote:
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.​
 

--
----------
Brian Coca

James Martin

unread,
Mar 31, 2016, 12:32:25 AM3/31/16
to ansible...@googlegroups.com
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
     





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/08886f97-31ee-47eb-8797-ee094645e4f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages