<p><em>Note: this answer will very likely and quickly become obsolete when a new version of the <a href="https://github.com/ansible-collections/community.mongodb" rel="nofollow noreferrer"><code>community.mongodb</code> collection</a> is released</em></p>
<hr />
<p>The current version (i.e. <em>v1.3.2</em> at time of this writing) is not compatible with the python module <code>pymongo</code> version <em>4.x</em>. You need to downgrade the library to version <em>3.x</em> until the collection is fixed.</p>
<p>The current <a href="https://github.com/ansible-collections/community.mongodb/commit/26c77072ff10a2a396ee309928d052c142811c7a" rel="nofollow noreferrer">latest commit</a> on the collection master branch is actually fixing the library version to <em>v3.12.2</em> but has not yet been relased. Hopefully this will soon make it into a patch version for the collection.</p>
<p>In the mean time, it seems that the developers have already started <a href="https://github.com/ansible-collections/community.mongodb/tree/pymongo4" rel="nofollow noreferrer">a branch for pymongo4 compatibiliy</a>. There is also <a href="https://github.com/ansible-collections/community.mongodb/issues/424" rel="nofollow noreferrer">a specific issue</a> related to this problem.</p>

CC BY-SA 4.0
2022-01-09T09:23:58.553Z
2022-01-09T09:23:58.553Z
Zeitounator
<p>I am learning ansible on my local vagrant vm machine and I am setting up mongodb using ansible and I keep getting the error:</p>
<pre><code>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! =&gt; {&quot;changed&quot;: false, &quot;module_stderr&quot;: &quot;Shared connection to 192.168.56.11 closed.\r\n&quot;, &quot;module_stdout&quot;: &quot;Traceback (most recent call last):\r\n  File \&quot;/home/vagrant/.ansible/tmp/ansible-tmp-1640759955.2153473-3678-226245830655855/AnsiballZ_mongodb_user.py\&quot;, line 107, in &lt;module&gt;\r\n    _ansiballz_main()\r\n  File \&quot;/home/vagrant/.ansible/tmp/ansible-tmp-1640759955.2153473-3678-226245830655855/AnsiballZ_mongodb_user.py\&quot;, line 99, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \&quot;/home/vagrant/.ansible/tmp/ansible-tmp-1640759955.2153473-3678-226245830655855/AnsiballZ_mongodb_user.py\&quot;, line 47, in invoke_module\r\n  ...
</code></pre>
<p>The anisble playbook configuration:</p>
<pre><code># 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 &quot;^auth = true&quot; /etc/mongodb.conf
  register: auth
  ignore_errors: yes
- name: Add an administrator to the db
  mongodb_user:
    database: admin
    name: root
    password: &quot;{{ dbpass }}&quot;
    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: &quot;{{ dbpass }}&quot;
    database: &quot;{{ dbname }}&quot;
    name: &quot;{{ appdbuser }}&quot;
    password: &quot;{{ appdbpass }}&quot;
    roles: dbOwner
    state: present
</code></pre>
<p>This part from my analysis of the error message is the issue:</p>
<pre><code>- name: Create the application user
  mongodb_user:
    login_user: root
    login_password: &quot;{{ dbpass }}&quot;
    database: &quot;{{ dbname }}&quot;
    name: &quot;{{ appdbuser }}&quot;
    password: &quot;{{ appdbpass }}&quot;
    roles: dbOwner
    state: present
</code></pre>
<p>My anisble machine specifications are:</p>
<ul>
<li><p>Ubuntu vm:</p>
<pre><code> 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
</code></pre>
</li>
<li><p>Installed ansible version on Ubuntu vm:</p>
<pre><code> 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
</code></pre>
</li>
</ul>

CC BY-SA 4.0
2022-01-07T18:24:48.157Z
This seems to be a [pretty common problem](https://www.qwant.com/?q=If+you+meant+to+call+the+'authenticate'+method+on+a+'Database'+object+it+is+failing+because+no+such+method+exists) related to the version of pymongo installed on the target.
Zeitounator
CC BY-SA 4.0
2022-01-08T22:49:20.093Z
Yes, I believe so and I was able to downgrade the pymongo version to 3.12.1 on the vm and it worked
George Udosen
CC BY-SA 4.0
2022-01-09T08:59:43.943Z
The [latest commit](https://github.com/ansible-collections/community.mongodb/commit/26c77072ff10a2a396ee309928d052c142811c7a) on their [github repo](https://github.com/ansible-collections/community.mongodb) 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.
Zeitounator
<ansible><mongodb>
Ansible playbook error: Collection' object is not callable
George Udosen
# Ansible playbook error: Collection' object is not callable

(Asked by: username_0 on 2022-01-07)

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 \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

**Tags:** ansible, mongodb

> **Comments:**
> **username_1**: This seems to be a [pretty common problem](https://www.qwant.com/?q=If+you+meant+to+call+the+'authenticate'+method+on+a+'Database'+object+it+is+failing+because+no+such+method+exists) related to the version of pymongo installed on the target.  
> **username_0**: Yes, I believe so and I was able to downgrade the pymongo version to 3.12.1 on the vm and it worked  
> **username_1**: The [latest commit](https://github.com/ansible-collections/community.mongodb/commit/26c77072ff10a2a396ee309928d052c142811c7a) on their [github repo](https://github.com/ansible-collections/community.mongodb) 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.  

---

## 1 answers

### Answer by username_1 on 2022-01-09. Score: 3 | Accepted answer

*Note: this answer will very likely and quickly become obsolete when a new version of the [`community.mongodb` collection](https://github.com/ansible-collections/community.mongodb) 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](https://github.com/ansible-collections/community.mongodb/commit/26c77072ff10a2a396ee309928d052c142811c7a) 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](https://github.com/ansible-collections/community.mongodb/tree/pymongo4). There is also [a specific issue](https://github.com/ansible-collections/community.mongodb/issues/424) related to this problem.


---

