3

I am learning ansible on my local vagrant vm machine and I am setting up mongodb using ansible and I keep getting the error:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: 'Collection' object is not callable. If you meant to call the 'authenticate' method on a 'Database' object it is failing because no such method exists.
fatal: [192.168.56.11]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 192.168.56.11 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1640759955.2153473-3678-226245830655855/AnsiballZ_mongodb_user.py\", line 107, in <module>\r\n    _ansiballz_main()\r\n  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1640759955.2153473-3678-226245830655855/AnsiballZ_mongodb_user.py\", line 99, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1640759955.2153473-3678-226245830655855/AnsiballZ_mongodb_user.py\", line 47, in invoke_module\r\n  ...

The anisble playbook configuration:

# tasks file for mongodb
- include_vars: secrets.yml
- name: Installing mongodb
  apt:
   name: mongodb
   state: present
- name: Start and enable the engine
  service:
    name: mongodb
    state: started
    enabled: yes
- name: Configure Mongodb to accept outside connections
  lineinfile:
    path: /etc/mongodb.conf
    regexp: '^#?bind_ip = 127.0.0.1'
    line: '#bind_ip = 127.0.0.1'
  notify:
     - restart mongodb
- name: Update Ubuntu
  apt:  update_cache=yes force_apt_get=yes cache_valid_time=3600
- name: Install Python pip
  apt:
    name: python3-pip
    state: present
- name: Install pymongo package
  pip:
    name: pymongo
    state: present
- name: Is authentication enabled?
  command: grep "^auth = true" /etc/mongodb.conf
  register: auth
  ignore_errors: yes
- name: Add an administrator to the db
  mongodb_user:
    database: admin
    name: root
    password: "{{ dbpass }}"
    roles: root
    state: present
  when: auth.rc == 1
- name: Enable authentication
  lineinfile:
    path: /etc/mongodb.conf
    regexp: '^#?auth \= true'
    line: 'auth = true'
  notify:
    - restart mongodb
- name: Create the application user
  mongodb_user:
    login_user: root
    login_password: "{{ dbpass }}"
    database: "{{ dbname }}"
    name: "{{ appdbuser }}"
    password: "{{ appdbpass }}"
    roles: dbOwner
    state: present

This part from my analysis of the error message is the issue:

- name: Create the application user
  mongodb_user:
    login_user: root
    login_password: "{{ dbpass }}"
    database: "{{ dbname }}"
    name: "{{ appdbuser }}"
    password: "{{ appdbpass }}"
    roles: dbOwner
    state: present

My anisble machine specifications are:

  • Ubuntu vm:

     Linux ubuntu 5.4.0-80-generic #90-Ubuntu SMP Fri Jul 9 22:49:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
     Ubuntu 20.04.2 LTS
    
  • Installed ansible version on Ubuntu vm:

     ansible [core 2.12.1]
        config file = /etc/ansible/ansible.cfg
        configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
        ansible python module location = /usr/lib/python3/dist-packages/ansible
        ansible collection location = /home/vagrant/.ansible/collections:/usr/share/ansible/collections
        executable location = /usr/bin/ansible
        python version = 3.8.10 (default, Jun  2 2021, 10:49:15) [GCC 9.4.0]
        jinja version = 2.10.1
        libyaml = True
    
3
  • 1
    This seems to be a pretty common problem related to the version of pymongo installed on the target. Commented Jan 7, 2022 at 18:24
  • Yes, I believe so and I was able to downgrade the pymongo version to 3.12.1 on the vm and it worked Commented Jan 8, 2022 at 22:49
  • The latest commit on their github repo has not yet been released and is fixing the pymongo version to 3.12.2. It should be part of the next patch version I guess and hopefully the next minor/major version will work with latest version of the python module. Commented Jan 9, 2022 at 8:59

1 Answer 1

3

Note: this answer will very likely and quickly become obsolete when a new version of the community.mongodb collection is released


The current version (i.e. v1.3.2 at time of this writing) is not compatible with the python module pymongo version 4.x. You need to downgrade the library to version 3.x until the collection is fixed.

The current latest commit on the collection master branch is actually fixing the library version to v3.12.2 but has not yet been relased. Hopefully this will soon make it into a patch version for the collection.

In the mean time, it seems that the developers have already started a branch for pymongo4 compatibiliy. There is also a specific issue related to this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.