Discussion:
[ansible-project] Importing custom python library from ansible custom module
Olivier Lauret
2015-07-16 07:33:21 UTC
Permalink
Hello all,

Is there a way to import a custom python library from an ansible custom
module?

I am trying to create a custom module and need a specific python library. I
have put my inifile.py file in the library folder of my playbook. My
ansible custom module is in the same folder. However, when using the import
command (from inifile import IniFile) in the custom module, it cannot find
the pyhton library:

invalid output was: Traceback (most recent call last):
File
"/home/olivier/.ansible/tmp/ansible-tmp-1437029018.2-53200658786921/file_ini_correct_options",
line 92, in <module>
from inifile import IniFile
ImportError: No module named inifile

Any way to do that?

Regards,
Olivier
--
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-project+***@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/f62e78a7-97e4-40df-94b6-87accc4014be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Timothy Appnel
2015-07-16 14:04:37 UTC
Permalink
In a previous life I've had to do what you are attempting. Ansible will not
automatically know to install dependencies like your custom library for
you.That is up to you to handle. There are two ways I've gone about it
resolving this:

1) Install said libraries in your default python library path on each
remote box.
2) Copy/install your libraries to an alternate path location (could be one
time could be an earlier task of the play) and use the environment argument
on the task that uses your custom module to append said library path to
PYTHONPATH.

Not knowing what is in your infile.py, you may want to consider embedding
that library in the module if it's a single library file that's not too
complex. I didn't personally use this approach, though I considered it, in
my own because the library files had other usage outside of my custom
Ansible module and I didn't want to play around keeping the code in sync.

Hope that helps.

<tim/>
--
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-project+***@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/CAOMCme63ZpWdD9nOzgBZvNKpW29zkAQUYwG8t8oSnMXYUb%3DOuw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Martin
2015-07-16 14:14:26 UTC
Permalink
Hi,
Post by Timothy Appnel
In a previous life I've had to do what you are attempting.
So what do you do now? It sound like you went on and are using a completely
different approach to achieve what the op wants to do (load custom code)

I'm not asking about options but rather what you actually ended up with
doing (give up and/or stopped using the tool are viable answers to me)

/Martin
--
--
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
Mobil: +43 / 660 / 62 45 103
UID: ATU68801424
--
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-project+***@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/CAK1mKEQxjUW0kB9Yb426THm8HZCMOy%3DbdbCzztjARScFrYsKzQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Timothy Appnel
2015-07-16 14:19:56 UTC
Permalink
I didn't give up at all. Far from it! I started working for Ansible.

Last I spoke to my former co-workers they are still using options 2 that I
setup because they do not have the ability to install python libraries in
the default library path.

<tim/>
Post by Martin
Hi,
Post by Timothy Appnel
In a previous life I've had to do what you are attempting.
So what do you do now? It sound like you went on and are using a
completely different approach to achieve what the op wants to do (load
custom code)
I'm not asking about options but rather what you actually ended up with
doing (give up and/or stopped using the tool are viable answers to me)
/Martin
--
--
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
Mobil: +43 / 660 / 62 45 103
UID: ATU68801424
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAK1mKEQxjUW0kB9Yb426THm8HZCMOy%3DbdbCzztjARScFrYsKzQ%40mail.gmail.com
<https://groups.google.com/d/msgid/ansible-project/CAK1mKEQxjUW0kB9Yb426THm8HZCMOy%3DbdbCzztjARScFrYsKzQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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-project+***@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/CAOMCme7JjHtUYE%3D4DRSyCd1tsEL19w0-1BRSnunD9xHbfc-8EQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Olivier Lauret
2015-07-16 19:30:38 UTC
Permalink
Hi Tim,

Thank you very much for the update. It helps a lot, thank you very much.
Indeed, I have chosen your second option as it is easier to maintain and
avoid any conflict with other systems. The inifile.py is too big to be
included in the custom module but I would not do this as I wouldn't be able
to re-utilise it.

Here is how I did it:

In my playbook or role:
tasks:
- name: Upload python module
copy: src=library/inifile.py dest=/tmp/inifile.py
In my custom module:
import sys
sys.path.append('/tmp')
from inifile import IniFile

And to answer your question, what I am trying to achieve here is an
extension to the ini_file module in Ansible. The current module doesn't
answer my needs such as:

- setting easily a continious list of options such as:
- whitelist.0 = item1
- whitelist.1 = item2
- whitelist.2 = item3
- checking and replacing a list of options (such as ["ower", "owne"]) by
one option name ("owner")
- Dealing with options that doesn't have section (generally mean that
this is default to all sections)
- Dealing with empty name section ([]) and comments - this one I am less
sure about but I know that the ConfigParser cannot handle this.

Thank you again for your precious help.


Regards,

Olivier
Post by Timothy Appnel
I didn't give up at all. Far from it! I started working for Ansible.
Last I spoke to my former co-workers they are still using options 2 that I
setup because they do not have the ability to install python libraries in
the default library path.
<tim/>
Post by Martin
Hi,
Post by Timothy Appnel
In a previous life I've had to do what you are attempting.
So what do you do now? It sound like you went on and are using a
completely different approach to achieve what the op wants to do (load
custom code)
I'm not asking about options but rather what you actually ended up with
doing (give up and/or stopped using the tool are viable answers to me)
/Martin
--
--
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
Mobil: +43 / 660 / 62 45 103
UID: ATU68801424
--
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
<javascript:>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAK1mKEQxjUW0kB9Yb426THm8HZCMOy%3DbdbCzztjARScFrYsKzQ%40mail.gmail.com
<https://groups.google.com/d/msgid/ansible-project/CAK1mKEQxjUW0kB9Yb426THm8HZCMOy%3DbdbCzztjARScFrYsKzQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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-project+***@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/a7ac54d2-e808-4c46-855e-5f3326b5b069%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Timothy Appnel
2015-07-16 19:53:11 UTC
Permalink
I'm glad you were able to work something out and I was helpful. A couple of
quick added thoughts on this.

I wouldn't recommend just dropping your library in to just /tmp to avoid a
potential conflict with another job or process. Perhaps create a separate
directory like the name of your project or playbook under tmp and store it
there? I think create a specific directory helps avoid this.

I also wouldn't recommend embedding a library path in your custom module
for maintainability. It's working for you now, but the thought of having to
have that path line up in two different places make me uneasy. I used the
environment arg and appended the path to our libraries to PYTHONPATH.
Everything is in one place: the playbook. A var to hold the path (you can
use Ansible facts to really create a unique path that won't get tread on),
the path create, the copy, the environment setting. Change it in one place
and the change probates thru.

Just some food for thought.

<tim/>


On Thu, Jul 16, 2015 at 3:30 PM, Olivier Lauret <
Post by Olivier Lauret
Hi Tim,
Thank you very much for the update. It helps a lot, thank you very much.
Indeed, I have chosen your second option as it is easier to maintain and
avoid any conflict with other systems. The inifile.py is too big to be
included in the custom module but I would not do this as I wouldn't be able
to re-utilise it.
- name: Upload python module
copy: src=library/inifile.py dest=/tmp/inifile.py
import sys
sys.path.append('/tmp')
from inifile import IniFile
And to answer your question, what I am trying to achieve here is an
extension to the ini_file module in Ansible. The current module doesn't
- whitelist.0 = item1
- whitelist.1 = item2
- whitelist.2 = item3
- checking and replacing a list of options (such as ["ower", "owne"])
by one option name ("owner")
- Dealing with options that doesn't have section (generally mean that
this is default to all sections)
- Dealing with empty name section ([]) and comments - this one I am
less sure about but I know that the ConfigParser cannot handle this.
Thank you again for your precious help.
Regards,
Olivier
Post by Timothy Appnel
I didn't give up at all. Far from it! I started working for Ansible.
Last I spoke to my former co-workers they are still using options 2 that
I setup because they do not have the ability to install python libraries in
the default library path.
<tim/>
Post by Martin
Hi,
Post by Timothy Appnel
In a previous life I've had to do what you are attempting.
So what do you do now? It sound like you went on and are using a
completely different approach to achieve what the op wants to do (load
custom code)
I'm not asking about options but rather what you actually ended up with
doing (give up and/or stopped using the tool are viable answers to me)
/Martin
--
--
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
Mobil: +43 / 660 / 62 45 103
UID: ATU68801424
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAK1mKEQxjUW0kB9Yb426THm8HZCMOy%3DbdbCzztjARScFrYsKzQ%40mail.gmail.com
<https://groups.google.com/d/msgid/ansible-project/CAK1mKEQxjUW0kB9Yb426THm8HZCMOy%3DbdbCzztjARScFrYsKzQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/a7ac54d2-e808-4c46-855e-5f3326b5b069%40googlegroups.com
<https://groups.google.com/d/msgid/ansible-project/a7ac54d2-e808-4c46-855e-5f3326b5b069%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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-project+***@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/CAOMCme6p-sG1seLEiZM%3DNppQueP0xoYb0Gps0NV81q-XvDRvFw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Olivier Lauret
2015-07-16 20:33:54 UTC
Permalink
Hi Tim,

Very good point. I will definitely change it accordingly. Thank you again.

Regards,
Olivier
Post by Timothy Appnel
I'm glad you were able to work something out and I was helpful. A couple
of quick added thoughts on this.
I wouldn't recommend just dropping your library in to just /tmp to avoid a
potential conflict with another job or process. Perhaps create a separate
directory like the name of your project or playbook under tmp and store it
there? I think create a specific directory helps avoid this.
I also wouldn't recommend embedding a library path in your custom module
for maintainability. It's working for you now, but the thought of having to
have that path line up in two different places make me uneasy. I used the
environment arg and appended the path to our libraries to PYTHONPATH.
Everything is in one place: the playbook. A var to hold the path (you can
use Ansible facts to really create a unique path that won't get tread on),
the path create, the copy, the environment setting. Change it in one place
and the change probates thru.
Just some food for thought.
<tim/>
Post by Olivier Lauret
Hi Tim,
Thank you very much for the update. It helps a lot, thank you very much.
Indeed, I have chosen your second option as it is easier to maintain and
avoid any conflict with other systems. The inifile.py is too big to be
included in the custom module but I would not do this as I wouldn't be able
to re-utilise it.
- name: Upload python module
copy: src=library/inifile.py dest=/tmp/inifile.py
import sys
sys.path.append('/tmp')
from inifile import IniFile
And to answer your question, what I am trying to achieve here is an
extension to the ini_file module in Ansible. The current module doesn't
- whitelist.0 = item1
- whitelist.1 = item2
- whitelist.2 = item3
- checking and replacing a list of options (such as ["ower", "owne"])
by one option name ("owner")
- Dealing with options that doesn't have section (generally mean that
this is default to all sections)
- Dealing with empty name section ([]) and comments - this one I am
less sure about but I know that the ConfigParser cannot handle this.
Thank you again for your precious help.
Regards,
Olivier
Post by Timothy Appnel
I didn't give up at all. Far from it! I started working for Ansible.
Last I spoke to my former co-workers they are still using options 2 that
I setup because they do not have the ability to install python libraries in
the default library path.
<tim/>
Post by Martin
Hi,
Post by Timothy Appnel
In a previous life I've had to do what you are attempting.
So what do you do now? It sound like you went on and are using a
completely different approach to achieve what the op wants to do (load
custom code)
I'm not asking about options but rather what you actually ended up with
doing (give up and/or stopped using the tool are viable answers to me)
/Martin
--
--
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
Mobil: +43 / 660 / 62 45 103
UID: ATU68801424
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAK1mKEQxjUW0kB9Yb426THm8HZCMOy%3DbdbCzztjARScFrYsKzQ%40mail.gmail.com
<https://groups.google.com/d/msgid/ansible-project/CAK1mKEQxjUW0kB9Yb426THm8HZCMOy%3DbdbCzztjARScFrYsKzQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
<javascript:>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/a7ac54d2-e808-4c46-855e-5f3326b5b069%40googlegroups.com
<https://groups.google.com/d/msgid/ansible-project/a7ac54d2-e808-4c46-855e-5f3326b5b069%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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-project+***@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/d741a709-3689-4e0b-8991-f9f9d0340474%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Toshio Kuratomi
2015-07-20 15:51:17 UTC
Permalink
Post by Timothy Appnel
I wouldn't recommend just dropping your library in to just /tmp to avoid
a potential conflict with another job or process. Perhaps create a separate
directory like the name of your project or playbook under tmp and store it
there? I think create a specific directory helps avoid this.
Yes, using bare /tmp is also a security risk (as anyone with access to the
machine can put something in /tmp. Imagine if someone puts their own
version of os.py in there, for instance). A subdirectory with proper mode
bits set works. A subdirectory in a non-world-writable location is even
better.

-Toshio
--
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-project+***@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/CAG9juEoFr_%2BFJ3z-GNpShaDxhELzknbOn1GuMhRdws_jJs57%2BA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...