how do I create a collection in mongoDB using an Ansible playbook? - mongodb

I'm using Ansible to configure a Vagrant VM that, on vagrant up, starts mongoDB and creates a database and collection ready to receive data. Another process will write data to the mongoDB collection. Here's a snippet from the playbook:
- hosts: all
user: vagrant
sudo: True
tasks:
- name: install packages
action: apt update_cache=yes pkg={{item}} state=installed
with_items:
- mongodb
- service: name=mongodb state=started
- shell: mongo mydb --eval "db.createCollection('mycollection')"
The playbook installs and starts the mongoDB service. However, it doesn't create the database/collection. There's something wrong with the "- shell: mongo ..." line. It returns:
TASK: [shell mongo mydb --eval "db.createCollection('mycollection')"] ****
failed: [default] => {"changed": true, "cmd": "mongo mydb --eval\"db.createCollection('mycollection')\" ", "delta": "0:00:00.084196", "end": "2014-08-29 04:48:40.333460", "rc": 255, "start": "2014-08-29 04:48:40.249264"}
stderr: exception: connect failed
stdout: MongoDB shell version: 2.0.4
connecting to: mydb
Fri Aug 29 04:48:40 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
Can you see what I did wrong?

Your approach should be correct. I just tested your mongo line on a vagrant booted instance with mongo installed and it worked fine.
Can you check the mongo logs to see what it is complaining about? If you can't connect, it could be that the mongo service is binding to ipv6 addr, binding to wrong port (should be 27017), or their are some authentication issues.
After checking that, can you boot the machine, get on the shell, then execute this bit:
mongo mydb --eval "db.createCollection('mycollection')"
Does it work from the shell? (run as same user as Ansible).

Related

approach to migrate mongodb using mongomirror using 2 GCE only

approach to migrate mongodb using mongomirror: Setting up mongodb instace on GCE and then migrate to another on GCE only
setting up 2 GCE on same project
Installed mongodb on both GCE
Installed mongomirror on source vm1
Tried exeuting mongomirror command on source GCE using username and pwd
./mongomirror --host rsMigration/SOURCE_IP --username root --password root123 --destination DESTINATION_IP:27017 --destinationUsername admin --destinationPassword admin123
Output:
mongomirror version: 0.12.2
git version: 9b30f320e44f90afec5c0fa792d4c003b71ead6b
Go version: go1.16.7
os: linux
arch: amd64
compiler: gc
2021-10-27T07:57:03.252+0000 Error initializing mongomirror: could not initialize source connection: could not connect to server: server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: 27017:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp 0.0.105.137:27017: i/o timeout }, ] }

Error: Authentication failed in mongodb 4.4.4 running in docker

I used to run mongodb in docker with this config
~/project/.env file is
# Mongodb common config
CONFIG_MONGODB_PORT=27020
CONFIG_MONGODB_PREFIX=mongodb://
CONFIG_MONGODB_AUTH_ENABLED=true
CONFIG_MONGODB_USERNAME=alok
CONFIG_MONGODB_PASSWORD=alokm
CONFIG_MONGODB_AUTH_SOURCE=admin
~/project/docker-compose.yml file is
version: '3.4'
mongodb:
image: mongo:4.0.3
restart: unless-stopped
command: mongod --port ${CONFIG_MONGODB_PORT}
volumes:
- ${HOST_MONGODB_DATA}:/data/db
ports:
- 5010:${CONFIG_MONGODB_PORT}
expose:
- ${CONFIG_MONGODB_PORT}
environment:
MONGO_INITDB_ROOT_USERNAME: ${CONFIG_MONGODB_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${CONFIG_MONGODB_PASSWORD}
To run mongodb
sudo docker-compose up --build
To access mongodb from my laptop
mongo localhost:5010 --password alokm --username alok --authenticationDatabase admin
This all setup used to work fine without any extra setup.
But when I changed mongodb image from mongo:4.0.3 to mongo:4.4.4 and run freshly on another laptop I see
ubuntu#myhost:~/project$ mongo localhost:5010 --password alokm --username alok --authenticationDatabase admin
MongoDB shell version v3.6.8
connecting to: mongodb://localhost:5010/test
Implicit session: session { "id" : UUID("4282986e-eb0b-4a88-8d39-1295192a66e7") }
MongoDB server version: 4.4.4
WARNING: shell and server versions do not match
2021-04-16T01:41:18.225+0000 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1608:20
#(auth):6:1
#(auth):1:2
exception: login failed
Why my this setup is not working in case of image: mongo:4.4.4 like it was working in case of image: mongo:4.0.3?

Error when Adding postgresql user named odoo in remote node(docker containner) with ansible

I'm working to add postgresql user named odoo in remote node (docker container) with ansible knowing that I'm trying to follow the manual installation:
$sudo apt-get install postgresql-9.6
$sudo su postgres
$cd
$createuser -s odoo
and this is my code
- name: Instalation of postgresql-9.6 server
apt:
name: postgresql-9.6
state: latest
- name: Ensure the PostgreSQL service is running
service: name=postgresql state=started enabled=yes
- name: Create odoo user
become_user: postgres
postgresql_user:
state: present
login_user: postgres
name: odoo
password: odoo
role_attr_flags: "SUPERUSER,CREATEDB"
priv: "CONNECT/products:ALL"
after running i faced this error
PLAY [My Odoo] ***************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************
ok: [172.17.0.5]
TASK [Postgresql : Instalation of postgresql-9.6 server] *********************************************************************************************
ok: [172.17.0.5]
TASK [Postgresql : Ensure the PostgreSQL service is running] *****************************************************************************************
ok: [172.17.0.5]
TASK [Postgresql : Create odoo user] *****************************************************************************************************************
fatal: [172.17.0.5]: FAILED! => {"changed": false, "msg": "privileges require a database to be specified"}
to retry, use: --limit #/home/fedora/Desktop/ansible-test/playbook.retry
PLAY RECAP *******************************************************************************************************************************************
172.17.0.5 : ok=3 changed=0 unreachable=0 failed=1
Can you help me resolving this?
You've specified a priv option, but haven't specified a database, which should be affected by your config. In your specific case, since you just want to create a user you should omit priv: to have something like
- name: Create odoo user
become_user: postgres
postgresql_user:
state: present
login_user: postgres
name: odoo
password: odoo
role_attr_flags: "SUPERUSER,CREATEDB"

Unable to set up default credentials for official Mongo docker image

I am using docker-compose to launch an official mongodb database container.
Reading the scripts and some StackOverflow questions looks like MONGO_INITDB_ environment variables can be used to set default credentials.
After running the following docker-compose.yml:
version: '2'
services:
mongodb:
image: mongo:3.6
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=collection
ports:
- 27017:27017
And trying to connect with mongo cli or mongoose Node.js ODM I get authentication error:
➔ mongo -u user -p password --verbose localhost:27017/collection
MongoDB shell version: 2.6.10
connecting to: localhost:27017/collection
2018-04-10T12:29:46.386+0200 creating new connection to:localhost:27017
2018-04-10T12:29:46.386+0200 [ConnectBG] BackgroundJob starting: ConnectBG
2018-04-10T12:29:46.387+0200 connected to server localhost:27017 (127.0.0.1)
2018-04-10T12:29:46.387+0200 connected connection!
2018-04-10T12:29:46.391+0200 User Assertion: 18:{ ok: 0.0, errmsg: "auth failed", code: 18, codeName: "AuthenticationFailed" }
2018-04-10T12:29:46.392+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18, codeName: "AuthenticationFailed" } at src/mongo/shell/db.js:1287
2018-04-10T12:29:46.392+0200 User Assertion: 12514:login failed
2018-04-10T12:29:46.393+0200 freeing 1 uncollected N5mongo12DBClientBaseE objects
exception: login failed
Connecting to /admin collection as defined in the entrypoint script [1] gave the same result. I am unable to see what am I doing wrong.
EDIT: Derick, from irc.freenode.org #mongodb channel, suggested that it may be not picking the --auth flag. I tried to log in without credentials (mongo --verbose localhost:27017/admin) and it worked. I am still having the issue of establishing defaults credentials, so issue is not yet solved.
[1] https://github.com/docker-library/mongo/blob/b96fddd1e1a100c01f0ea6d28e1c7ccc750fd5c0/3.6/docker-entrypoint.sh#L292

mongoDB : Authentication of user is working fine, but getting "Unauthorized not authorized on admin to execute command" in the logs

I've followed the steps as mentioned in How do I add an admin user to Mongo in 2.6?
At first, "auth=true" in the /etc/mongod.conf file is commented out so that authentication is not done and I could create the following users in respective dbs.
Admin:
use admin;
db.createUser({user: "mongoRoot", pwd: "password", roles: [{role: "root", db: "admin"}]});
db.createUser({user: "mongoAdmin", pwd: "password", roles: ["readWrite"]});
db.createUser({user: "siteUserAdmin", pwd: "password", roles: [{role: "userAdminAnyDatabase", db: "admin"}]});
db.createUser({user: "mongoDBAdmin", pwd: "password", roles: [{role: "dbAdmin", db: "admin"}]});
db.createUser({user: "mongoDBOwner", pwd: "password", roles: [{role: "dbOwner", db: "admin"}]});
db.createUser({user: "mongoWrite", pwd: "password", roles: [{role: "readWrite",db: "mongo_database"}]}); (Added in admin so that by giving the command from the command-line 'mongo mongo_database --port 27018 -u mongoWrite -p password --authenticationDatabase admin', the user mongoWrite is able to login as done in https://gist.github.com/tamoyal/10441108)
db.createUser({user: "mongoRead", pwd: "password", roles: [{role: "read", db: "mongo_database"}]}); (Added in admin so that by giving the command from the command-line 'mongo mongo_database --port 27018 -u mongoRead -p password --authenticationDatabase admin', the user mongoRead is able to login as done in https://gist.github.com/tamoyal/10441108)
Config:
use config;
db.createUser({user: "mongoConfig", pwd: "password", roles: [{role: "readWrite", db: "config"}]});
Test:
use test;
db.createUser({user: "mongoTest", pwd: "password", roles: [{role: "readWrite", db: "test"}]});
mongo_database:
use mongo_database;
db.createUser({user: "mongoWrite", pwd: "password", roles: [{role: "readWrite",db: "mongo_database"}]});
db.createUser({user: "mongoRead", pwd: "password", roles: [{role: "read", db: "mongo_database"}]});
db.createUser({user: "mongoAdmin", pwd: "password", roles: [{role: "readWrite", db: "mongo_database"}]});
After making sure that all the required users are added, turning on the authentication by uncommenting "auth=true" in the /etc/mongod.conf file and restarting the mongodb.
[ec2-user#ip-xxx-xx-xx-xx ~]$ mongo mongo_database --port 27018 -u mongoWrite -p password --authenticationDatabase admin
MongoDB shell version: 2.6.10
connecting to: 127.0.0.1:27018/mongo_database
rs0:PRIMARY> db.test.insert({"Hello":"World"});
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> exit
bye
[ec2-user#ip-xxx-xx-xx-xx ~]$ mongo mongo_database --port 27018 -u mongoRead -p password --authenticationDatabase admin
MongoDB shell version: 2.6.10
connecting to: 127.0.0.1:27018/mongo_database
rs0:PRIMARY> db.test.insert({"Hello":"World"});
WriteResult({
"writeError" : {
"code" : 13,
"errmsg" : "not authorized on mongo_database to execute command { insert: \"test\", documents: [ { _id: ObjectId('559bba6ead81843e121c5ac7'), Hello: \"World\" } ], ordered: true }"
}
})
rs0:PRIMARY>
Everything works fine till this point. The only issue that am encountering is that my log file is getting bombarded with the following 2 lines at almost tens of thousand lines per minute and within no time, my disk is running out of space.
2015-07-07T11:40:28.340+0000 [conn3] Unauthorized not authorized on admin to execute command { writebacklisten: ObjectId('55913d82b47aa336e4f971c2') }
2015-07-07T11:40:28.340+0000 [conn2] Unauthorized not authorized on admin to execute command { writebacklisten: ObjectId('55923232e292bbe6ca406e4e') }
Just to give an idea, in a span of 10 seconds, 10 MB worth of log file is generated consisting of just the above mentioned 2 lines.
[ec2-user#ip-xxx-xx-xx-xx ~]$ date
Tue Jul 7 11:44:01 UTC 2015
[ec2-user#ip-xxx-xx-xx-xx ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvdh 4.8G 388M 4.2G 9% /log
[ec2-user#ip-xxx-xx-xx-xx ~]$ date
Tue Jul 7 11:44:14 UTC 2015
[ec2-user#ip-xxx-xx-xx-xx ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvdh 4.8G 398M 4.2G 9% /log
To my knowledge, the authentication seems to be working fine. Just that the logs are getting filled at super sonic speed. What am I doing wrong? Please help. Thanks In Advance.
The excessive logging was from the config servers and even after adding the authentication to the config servers with authentication turned on, it wouldn't stop. Upgraded to mongo 3.0.4 for replica sets, turned on the authentication on replica sets and upgraded mongo to 3.0.4 on config servers and it started working fine without any issues (Same steps on mongo 2.6.x would result in the issue I mentioned above). So, we planned to upgrade to 3.0.4 in order to bypass this issue. Hope, it will be helpful to someone.