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.
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.
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.