include custom python library from custom module

217 views
Skip to first unread message

Alexey Wasilyev

unread,
Mar 21, 2016, 2:41:07 PM3/21/16
to ansible...@googlegroups.com
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

Uditha Desilva

unread,
Mar 22, 2016, 12:56:59 PM3/22/16
to Ansible Project
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.

Mike Biancaniello

unread,
Mar 22, 2016, 1:13:38 PM3/22/16
to Ansible Project
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.

Uditha Desilva

unread,
Mar 23, 2016, 7:43:19 AM3/23/16
to Ansible Project
Does that really work to copy over the dependent libraries to the target systems?

Mike Biancaniello

unread,
Mar 23, 2016, 8:23:04 AM3/23/16
to Ansible Project
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?

夏恺(Xia Kai)

unread,
Mar 23, 2016, 9:49:02 AM3/23/16
to Ansible Project
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.

Toshio Kuratomi

unread,
Mar 23, 2016, 10:15:44 AM3/23/16
to ansible...@googlegroups.com


On Mar 23, 2016 6:48 AM, "夏恺(Xia Kai)" <xia...@gmail.com> wrote:
>
> 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


>
> On Tuesday, March 22, 2016 at 2:41:07 AM UTC+8, Alexey Wasilyev wrote:
>>
>> 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
>

> --
> 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/bced9465-3175-4743-ade6-a1f071a6c5c4%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

夏 恺

unread,
Mar 23, 2016, 11:20:20 AM3/23/16
to ansible...@googlegroups.com
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. :)

夏 恺(Xia Kai)
http://blog.xiaket.org/
Reply all
Reply to author
Forward
0 new messages