include custom python library from custom module

Hello!

I am writing a set of custom ansible modules, that uses some shared code.
And cant find a right way how this can be organised. Where I should put my custom python library under current catalog so it can be imported by my modules?

I don’t want to makes “official” library, available via pip, and I don’t want to copy it manually out of current playbook tree.

Alexey

Given my understanding of the way Ansible bundles up modules, I am not sure that will work. You may need to install your custom python libraries on each of your target systems.

You can put them in the same or sub dir as your modules. Or, you can put them anywhere, just append the path in the module. However, if you want multiple modules in multiple roles to share the same libs, then things get tricky. I’ve done that by appending ‘…/…/pylibs/’ to my sys.path to get things up and running, but eventually just installed the libs globally on the system and did away with that ugliness. Of course, you could always just run ansible inside of a docker or python-virtualenv and then install the libs local to that environment.

Does that really work to copy over the dependent libraries to the target systems?

duh, right. It works for connection: local, but not for remote targets.

Maybe have a task that copies it to a tmp dir on the target and then append that to sys.path in your module?

I’m working on a similar problem, and I have to write an action plugin for it. As far as I know, and as much as I understand, there is no out of the box way to do this.

I’m working on a similar problem, and I have to write an action plugin for it. As far as I know, and as much as I understand, there is no out of the box way to do this.

You can copy the file into the module_utils directory in your ansible install. Then you should be able to use it similar to how most ansible modules use the basic.py module listed there. Note that we’re making some changes to how this works for 2.1 and (maybe for 2.1 but more likely for 2.2 due to the amount of time left before we want to release 2.1) will be looking at enabling a user directory where these sorts of small, custom libraries can live. (The new module loader code is talked about here: https://groups.google.com/forum/m/#!msg/ansible-project/0bwHEFfKOro/meU337AxCQAJ I don’t highlight user directories for custom code in the posts about it because the timing means that additional feature likely won’t make it into 2.1)

-Toshio

Thanks for the update, Toshio. Looking forward to the ziploader feature, very interesting idea.

What I've been working on is a little more difficult than this, I have to monkey patch some imports in existing code, so I have to add a strategy plugin and an action plugin for the job.

I'll see if I can do anything to help in your ziploader work tomorrow. :slight_smile:

夏 恺(Xia Kai)
http://blog.xiaket.org/