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