I'm using ansible to deploy mongodb-cluster
I connected to primary_node and created admin user in the admin database:
then updated /etc/mongo.conf to looks like this:
net:
port: 27017
bindIpAll: true
systemLog:
destination: file
logAppend: true
path: /log/mongod.log
storage:
dbPath: /data
journal:
enabled: true
security:
authorization: enabled
keyFile: /mongo_auth/mongodb.key
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
replication:
replSetName: s0
After this mongod process restarted.
Now I can login using admin/login credentials and I can run command to start replicaSet
rs.initiate({ _id: "s0", version: 1, members: [{ _id: 0, host: "stage-mongoprimary0.server.loc:27017", priority: 2 }, { _id: 1, host: "stage-mongosecondary1.server.loc:27017", priority: 1 }, { _id: 2, host: "stage-mongosecondary2.server.loc:27017", priority: 1 }] })
and everything works just fine. But I can't acomplish the same with ansible module for some reason. Here is my task
- name: Initialize MongoDB replica set
community.mongodb.mongodb_replicaset:
login_user: admin
login_password: "{{ admin_password }}"
replica_set: s0
members:
- _id: 0
host: stage-mongoprimary0.server.loc
priority: 2
- _id: 1
host: stage-mongosecondary1.server.loc
priority: 1
- _id: 2
host: stage-mongosecondary2.server.loc
priority: 1
when: inventory_hostname == mongo_primary
by default module uses admin db and default port which exactly what I already setup.
but when I run this task I get an error:
"msg": "Unable to create replica_set: Some problem not authorized on admin to execute command
But I can do the same if I login directly into mogo shell using the same creds. Please help.
admin role is root with highet privileges.
I'm using mongodb 3.6.23 pymongo 4.3.3 and Python3.8, ansible 2.9.23
this is what I see in logs:
2023-04-14T00:58:06.143+0000 I NETWORK [conn19] received client metadata from xx.xx.xx.xx:52300 conn19: { driver: { name: "PyMongo", version: "4.3.3" }, os: { type: "Linux", name: "Linux", architecture: "x86_64", version: "4.14.311-233.529.amzn2.x86_64" }, platform: "CPython 3.8.16.final.0" } 2023-04-14T00:58:06.144+0000 I ACCESS [conn19] Unauthorized: not authorized on admin to execute command { replSetInitiate: { _id: "s0", protocolVersion: 1, members: [ { _id: 0, host: "stage-mongoprimary0.server.loc:27017", priority: 2 }, { _id: 1, host: "stage-mongosecondary1.server.loc:27017", priority: 1 }, { _id: 2, host: "stage-mongosecondary2.server.loc:27017", priority: 1 } ], settings: { chainingAllowed: true, electionTimeoutMillis: 10000 } }, $db: "admin", $readPreference: { mode: "primaryPreferred" } }
already tried to assign following roles:
- { db: "admin", role: "userAdminAnyDatabase" }
- { db: "admin", role: "dbAdminAnyDatabase" }
- { db: "admin", role: "clusterAdmin" }
- { db: "admin", role: "readWriteAnyDatabase" }
even tried with
- { db: "admin", role: "__system" }
- { db: "admin", role: "root" }
still the same.
I can start ReplicaSet and add members only if I disable security which is not what I'm looking for.