python library path issues

Hi all,

Simple question–

something I’ve encountered before where python has no problem finding a module but ansible can’t find it:

line 132, in
from comware_5_2 import Comware_5_2
ImportError: No module named comware_5_2

But…

Type “help”, “copyright”, “credits” or “license” for more information.

from comware_5_2 import Comware_5_2

No problem. What am I missing? This is a library a module I wrote uses.

Regards,

Patrick

I’m going to guess that your which python is not /usr/bin/python

Ansible by default runs all modules using /usr/bin/python. If your default python is something like /usr/local/bin/python then chances are, that those 2 pythons do not share the same site-packages, and you may have installed that module using /usr/local/bin/python

You can try something like the following to verify:

which python -c “import comware_5_2; print comware_5_2.file
/usr/bin/python -c “import comware_5_2; print comware_5_2.file

To get ansible to use a different python, you can set ansible_python_interpreter. More information is available at <http://docs.ansible.com/faq.html#how-do-i-handle-python-pathing-not-having-a-python-2-x-in-usr-bin-python-on-a-remote-machine>

If you're using virtualenvs, you should prefer
#!/usr/bin/env python

to
#!/usr/bin/python

Thank you both for your replies.

I always do use virtualenv and always do use #!/usr/bin/env python

I think the situation is that I use the virtualenv.

How does one ensure that if they are running ansible in a virtualenv that any host it runs against has the same modules? It creates a temp dir on the host it runs on and that temp dir has everything that is needed, right? How does one make sure that temp dir has everything that is needed?

I solved the issue by installing the module systemically.

Regards,

Patrick

Are you saying that you solved the problem by installing the ComWare
module onto each machine that you are managing with ansible?

If so, ansible currently does not look at an ansible module's
dependencies and transfer those to the client. It looks at a few
special imports to determine if it needs to include code from within
ansible in the transfer but doesn't have a general facility to handle
other python modules (Look at ModuleReplacer
https://github.com/ansible/ansible/blob/devel/lib/ansible/module_common.py#L36
if you want details).

In the playbook I use to provision new boxes I use the ansible package
manager modules to install python module packages I know will be
needed on the client. Other people might install these via the tools
the OS provides for installing itself or as a first step in a playbook
that uses the python modules. If a module is not available in your
package manager, you can use the copy or synchronize modules to copy
the module over instead.

-Toshio