1

I'm trying to install PyMongo Python package with pip. It is required by Ansible mongodb_user module.

I'm installing pip and pymongo with following Ansible script:

- hosts: tag_Name_Development
  become: true
  remote_user: user
  tasks:
    - name: install python tools
      yum: name={{ item }} state=latest
      with_items:
        - gcc
        - python-devel
        - python-setuptools
        - python-pip

    - name: install pymongo
      pip: name=pymongo state=latest

    - name: add admin user to mongo
      mongodb_user:
        login_port: 27017
        database: admin
        name: admin
        password: "{{ mongodb.admin_pass }}"
        roles: userAdminAnyDatabase
        state: present

After successful installation of tools I get following Ansible error.

FAILED! => {"changed": false, "failed": true, "msg": "the python pymongo module is required"}

On server where pymongo is installed I get

$ python -c "import pymongo"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named pymongo

Other relevant info pip freeze and pip list

$ python-pip freeze
backports.ssl-match-hostname==3.4.0.2
ordereddict==1.2
pymongo==3.3.0

$ python-pip list
backports.ssl-match-hostname (3.4.0.2)
ordereddict (1.2)
pip (6.1.1)
pymongo (3.3.0)
setuptools (12.2)

And loaded paths

$ python
Python 2.7.10 (default, Jul 20 2016, 20:53:27)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/local/lib64/python2.7/site-packages', '/usr/local/lib/python2.7/site-packages', '/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages', '/usr/lib64/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

There are many other questions related to this problem, but none of those have helped. I do not have bson installed and I'm not using any virtual envs.

Niko Ruotsalainen
  • 49,514
  • 4
  • 24
  • 31

2 Answers2

0

It turned out that I have two versions of python installed (2.6 and 2.7). Even though default is 2.7, my Ansible script installed pymongo under 2.6 folder /usr/local/lib64/python2.6/site-packages

I found that out by running $ python-pip show pymongo and $ python2.6 -c "import pymongo" works as expected.

I changed my Ansible script to install python27-pip instead of python-pip and it started to work nicely.

Niko Ruotsalainen
  • 49,514
  • 4
  • 24
  • 31
0

I had similar problem. I solved it by appending PIP installation directory to PYTHON_PATH and changing permission of PIP installation directory.

I wrote the detailed answer here !

Community
  • 1
  • 1
Utsav Chokshi
  • 1,357
  • 1
  • 13
  • 35