I am am using ansible to add admin user in mongodb.. I used below playbook but i am getting error. Can someone suggest the solution.. i have also installed pymongo prior to adding user in order to use module. authentication is disabled in mongod.conf and bindIp is set to 0.0.0.0
# create mongoadmin user
- name: Create mongoadmin root user
#community.mongodb.mongodb_user:
mongodb_user:
login_port: 27017
database: "admin"
name: "mongoadmin"
password: "mongoadmin"
roles: "root"
ignore_errors: yes
notify:
- restart mongodb
I am getting below error
fatal: [devqa_mongod_single]: FAILED! => {"changed": false, "msg": "Unable to connect to database: Unknown option directconnection"}
CodePudding user response:
I assume you have a wrong configuration setting on the host.
Unable to connect to database: Unknown option directconnection
This doesn't look like an Ansible error to me.
To help you further out, you should disable mongo authentication, and restart mongo. Then, create 3 users, admin
, root
and userAdminAnyDatabase
. Then restart mongo. Here is an Ansible role I've written for MongoDB, so you can take a look there to see how it works.
CodePudding user response:
I create the users manually:
- hosts: all
vars:
mongoAuth: "/usr/bin/mongosh 'mongodb://admin:{{ password | urlencode() }}@localhost:27017/admin?authSource=admin' --norc --quiet"
mongoNoAuth: "/usr/bin/mongosh 'mongodb://localhost:27017/admin' --norc --quiet"
tasks:
- name: Check if authentication is enabled
shell:
cmd: "{{ mongoAuth }} --eval 'db.getMongo()'"
executable: /bin/bash
register: authenticate
failed_when: false
changed_when: false
check_mode: no
- name: Create users
shell:
cmd: "{{ (authenticate.rc == 0) | ternary(mongoAuth, mongoNoAuth) }} --eval '{{ js }}'"
executable: /bin/bash
vars:
js: |
admin = db.getSiblingDB("admin")
{% if authenticate.rc != 0 %}
admin.createUser({ user: "admin", pwd: "{{ password }}", roles: ["root"] })
admin.auth("admin", "{{ password }}")
{% endif %}
// create more users if needed
admin.createUser(...)