Support of mongodb 3

This commit is contained in:
Sergei Antipov 2016-04-17 19:43:00 +06:00
parent b6e2ffe362
commit 25c7cec6f2
23 changed files with 527 additions and 204 deletions

3
.gitignore vendored
View File

@ -1,5 +1,4 @@
.idea
*~
\#*\#
.\#*
.ropeproject/
/.python-version

41
.travis.yml Normal file
View File

@ -0,0 +1,41 @@
# .travis.yml
---
sudo: required
env:
- >
distribution=ubuntu-upstart
version=14.04
- >
distribution=ubuntu-upstart
version=12.04
- >
distribution=debian
version=7
services:
- docker
before_install:
- sudo apt-get update
# Latest Ansible install
- sudo apt-add-repository -y ppa:ansible/ansible
- sudo apt-get update -o Dir::Etc::sourcelist="sources.list.d/ansible-ansible-precise.list" \
-o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0"
- sudo apt-get install ansible python-pip -y
- sudo pip install docker-py
# Pull docker image
- docker pull ${distribution}:${version}
script:
- ansible-playbook -i tests/hosts tests/site.yml -e target=mongo1
# Idempotence test
- >
ansible-playbook -i tests/hosts tests/site.yml -e target=mongo1
| grep -q 'changed=0.*failed=0'
&& (echo 'Idempotence test: pass' && exit 0)
|| (echo 'Idempotence test: fail' && exit 1)
# Delete all containers
- docker kill mongo{1,2,3} && docker rm mongo{1,2,3}

View File

@ -1,7 +1,6 @@
Ansible role for MongoDB
============
This repository forked from [Stouts.mongodb](https://github.com/Stouts/Stouts.mongodb).
Ansible role which manage [MongoDB](http://www.mongodb.org/)
Ansible role which manage [MongoDB](http://www.mongodb.org/).
* Install and configure the MongoDB;
* Provide handlers for restart and reload;
@ -10,10 +9,20 @@ Ansible role which manage [MongoDB](http://www.mongodb.org/)
#### Variables
```yaml
# You can use this variable to control installation source of MongoDB
# 'mongodb' will be installed from Debian/Ubuntu repos
# 'mongodb-org' will be installed from MongoDB official repos
mongodb_package: mongodb-org
# You can control installed version via this param.
# Should be '2.6', '3.0' or '3.2'. This role does't support MongoDB < 2.4.
# I will recommend you to use latest version of MongoDB.
mongodb_version: "3.2"
mongodb_force_wait_for_port: false # When not forced, the role will wait for mongod port to become available only with systemd
mongodb_pymongo_from_pip: false # Install latest PyMongo via PIP or package manager
mongodb_pymongo_from_pip: true # Install latest PyMongo via PIP or package manager
mongodb_pymongo_pip_version: "3.2.2"
mongodb_disable_thp: true
mongodb_manage_service: true
@ -22,32 +31,45 @@ mongodb_user: mongodb
mongodb_uid:
mongodb_gid:
mongodb_daemon_name: "{{ 'mongod' if ('mongodb-org' in mongodb_package) else 'mongodb' }}"
## net Options
mongodb_net_bindip: 127.0.0.1 # Comma separated list of ip addresses to listen on
mongodb_net_http_enabled: false # Enable http interface
mongodb_net_ipv6: false # Enable IPv6 support (disabled by default)
mongodb_net_maxconns: 65536 # Max number of simultaneous connections
mongodb_net_port: 27017 # Specify port number
mongodb_conf_auth: false # Run with security
mongodb_conf_bind_ip: 127.0.0.1 # Comma separated list of ip addresses to listen on
mongodb_conf_cpu: true # Periodically show cpu and iowait utilization
mongodb_conf_dbpath: /data/db # Directory for datafiles
mongodb_conf_fork: false # Fork server process
mongodb_conf_httpinterface: false # Enable http interface
mongodb_conf_ipv6: false # Enable IPv6 support (disabled by default)
mongodb_conf_journal: true # Enable journaling
mongodb_conf_logappend: true # Append to logpath instead of over-writing
mongodb_conf_logpath: /var/log/mongodb/{{ mongodb_daemon_name }}.log # Log file to send write to instead of stdout
mongodb_conf_maxConns: 1000000 # Max number of simultaneous connections
mongodb_conf_noprealloc: false # Disable data file preallocation
mongodb_conf_smallfiles: false # Disable smallfiles option
mongodb_conf_noscripting: false # Disable scripting engine
mongodb_conf_notablescan: false # Do not allow table scans
mongodb_conf_port: 27017 # Specify port number
mongodb_conf_quota: false # Limits each database to a certain number of files
mongodb_conf_quotaFiles: 8 # Number of quota files
mongodb_conf_syslog: false # Log to system's syslog facility instead of file
## processManagement Options
mongodb_processmanagement_fork: false # Fork server process
# Replica set options:
mongodb_conf_replSet: # Enable replication <setname>[/<optionalseedhostlist>]
mongodb_conf_replIndexPrefetch: "all" # specify index prefetching behavior (if secondary) [none|_id_only|all]
mongodb_conf_oplogSize: 512 # specifies a maximum size in megabytes for the replication operation log
mongodb_conf_keyFile: /etc/mongodb-keyfile # Specify path to keyfile with password for inter-process authentication
## security Options
# Disable or enable security. Possible values: 'disabled', 'enabled'
mongodb_security_authorization: "disabled"
mongodb_security_keyfile: /etc/mongodb-keyfile # Specify path to keyfile with password for inter-process authentication
## storage Options
mongodb_storage_dbpath: /data/db # Directory for datafiles
# The storage engine for the mongod database. Available values:
# 'mmapv1', 'wiredTiger'
mongodb_storage_engine: "{{ 'mmapv1' if mongodb_version[0:3] == '3.0' else 'wiredTiger' }}"
# mmapv1 specific options
mongodb_storage_quota_enforced: false # Limits each database to a certain number of files
mongodb_storage_quota_maxfiles: 8 # Number of quota files per DB
mongodb_storage_smallfiles: false # Very useful for non-data nodes
mongodb_storage_journal_enabled: true # Enable journaling
mongodb_storage_prealloc: true # Disable data file preallocation
## systemLog Options
## The destination to which MongoDB sends all log output. Specify either 'file' or 'syslog'.
## If you specify 'file', you must also specify mongodb_systemlog_path.
mongodb_systemlog_destination: "file"
mongodb_systemlog_logappend: true # Append to logpath instead of over-writing
mongodb_systemlog_path: /var/log/mongodb/{{ mongodb_daemon_name }}.log # Log file to send write to instead of stdout
## replication Options
mongodb_replication_replset: # Enable replication <setname>[/<optionalseedhostlist>]
mongodb_replication_replindexprefetch: "all" # specify index prefetching behavior (if secondary) [none|_id_only|all]
mongodb_replication_oplogsize: 1024 # specifies a maximum size in megabytes for the replication operation log
# MMS Agent
mongodb_mms_agent_pkg: https://mms.mongodb.com/download/agent/automation/mongodb-mms-automation-agent-manager_1.4.2.783-1_amd64.deb
@ -99,7 +121,7 @@ Add `greendayonfire.mongodb` to your roles and set vars in your playbook file.
Example vars for authorization:
```yaml
mongodb_conf_auth: true
mongodb_security_authorization: true
mongodb_users:
- {
name: testUser,
@ -112,23 +134,34 @@ Required vars to change on production:
```yaml
mongodb_user_admin_password
mongodb_root_admin_password
# if you use replication and authorization
mongodb_security_keyfile
```
Example vars for replication:
```yaml
# It's a 'master' node
mongodb_login_host: 192.168.56.2
# mongodb_replication_params should be configured on each replica set node
mongodb_replication_params:
- { host_name: 192.168.56.2, host_port: "{{ mongodb_conf_port }}", host_type: replica }
- { host_name: 192.168.56.2, host_port: "{{ mongodb_net_port }}", host_type: replica }
# host_type can be replica(default) and arbiter
```
And inventory file for replica set:
```ini
[mongo_master]
192.158.56.2 mongodb_master=True # it'n not a really master of MongoDB replica set,
192.158.56.2 mongodb_master=True # it is't a really master of MongoDB replica set,
# use this variable for replica set init only
# or when master is moved from initial master node
[mongo_replicas]
192.168.56.3
192.168.56.4
[mongo:children]
mongo_master
mongo_replicas
```
Licensed under the GPLv2 License. See the [LICENSE.md](LICENSE.md) file for details.

View File

@ -1,14 +1,14 @@
---
mongodb_package: mongodb-org
mongodb_version: "2.6"
mongodb_apt_key_url: http://docs.mongodb.org/10gen-gpg-key.asc
mongodb_apt_key_id: 7F0CEB10
mongodb_force_wait_for_port: false
mongodb_pymongo_from_pip: false # Install latest PyMongo via PIP or package manager
mongodb_pymongo_pip_version: "2.9"
mongodb_disable_thp: true
mongodb_version: "3.2"
mongodb_apt_keyserver: keyserver.ubuntu.com
mongodb_apt_key_id: "{{ 'EA312927' if mongodb_version[0:3] == '3.2' else '7F0CEB10' }}"
mongodb_pymongo_from_pip: true # Install latest PyMongo via PIP or package manager
mongodb_force_wait_for_port: false
mongodb_disable_thp: true
mongodb_user_update_password: "on_create" # MongoDB user password update default policy
mongodb_manage_service: true
mongodb_user: mongodb
@ -16,31 +16,45 @@ mongodb_uid:
mongodb_gid:
mongodb_daemon_name: "{{ 'mongod' if ('mongodb-org' in mongodb_package) else 'mongodb' }}"
mongodb_conf_auth: false # Run with security
mongodb_conf_bind_ip: 127.0.0.1 # Comma separated list of ip addresses to listen on
mongodb_conf_cpu: true # Periodically show cpu and iowait utilization
mongodb_conf_dbpath: /data/db # Directory for datafiles
mongodb_conf_fork: false # Fork server process
mongodb_conf_httpinterface: false # Enable http interface
mongodb_conf_ipv6: false # Enable IPv6 support (disabled by default)
mongodb_conf_journal: true # Enable journaling
mongodb_conf_logappend: true # Append to logpath instead of over-writing
mongodb_conf_logpath: /var/log/mongodb/{{ mongodb_daemon_name }}.log # Log file to send write to instead of stdout
mongodb_conf_maxConns: 1000000 # Max number of simultaneous connections
mongodb_conf_noprealloc: false # Disable data file preallocation
mongodb_conf_smallfiles: false # Disable smallfiles option
mongodb_conf_noscripting: false # Disable scripting engine
mongodb_conf_notablescan: false # Do not allow table scans
mongodb_conf_port: 27017 # Specify port number
mongodb_conf_quota: false # Limits each database to a certain number of files
mongodb_conf_quotaFiles: 8 # Number of quota files
mongodb_conf_syslog: false # Log to system's syslog facility instead of file
## net Options
mongodb_net_bindip: 127.0.0.1 # Comma separated list of ip addresses to listen on
mongodb_net_http_enabled: false # Enable http interface
mongodb_net_ipv6: false # Enable IPv6 support (disabled by default)
mongodb_net_maxconns: 65536 # Max number of simultaneous connections
mongodb_net_port: 27017 # Specify port number
# Replica set options:
mongodb_conf_replSet: # Enable replication <setname>[/<optionalseedhostlist>]
mongodb_conf_replIndexPrefetch: "all" # specify index prefetching behavior (if secondary) [none|_id_only|all]
mongodb_conf_oplogSize: 512 # specifies a maximum size in megabytes for the replication operation log
mongodb_conf_keyFile: /etc/mongodb-keyfile # Specify path to keyfile with password for inter-process authentication
## processManagement Options
mongodb_processmanagement_fork: false # Fork server process
## security Options
# Disable or enable security. Possible values: 'disabled', 'enabled'
mongodb_security_authorization: "disabled"
mongodb_security_keyfile: /etc/mongodb-keyfile # Specify path to keyfile with password for inter-process authentication
## storage Options
mongodb_storage_dbpath: /data/db # Directory for datafiles
# The storage engine for the mongod database. Available values:
# 'mmapv1', 'wiredTiger'
mongodb_storage_engine: "{{ 'mmapv1' if mongodb_version[0:3] == '3.0' else 'wiredTiger' }}"
# mmapv1 specific options
mongodb_storage_quota_enforced: false # Limits each database to a certain number of files
mongodb_storage_quota_maxfiles: 8 # Number of quota files per DB
mongodb_storage_smallfiles: false # Very useful for non-data nodes
mongodb_storage_journal_enabled: true # Enable journaling
mongodb_storage_prealloc: true # Enable data file preallocation
## systemLog Options
## The destination to which MongoDB sends all log output. Specify either 'file' or 'syslog'.
## If you specify 'file', you must also specify mongodb_systemlog_path.
mongodb_systemlog_destination: "file"
mongodb_systemlog_logappend: true # Append to logpath instead of over-writing
mongodb_systemlog_path: /var/log/mongodb/{{ mongodb_daemon_name }}.log # Log file to send write to instead of stdout
## replication Options
mongodb_replication_replset: # Enable replication <setname>[/<optionalseedhostlist>]
mongodb_replication_replindexprefetch: "all" # specify index prefetching behavior (if secondary) [none|_id_only|all]
mongodb_replication_oplogsize: 1024 # specifies a maximum size in megabytes for the replication operation log
# MMS Agent
mongodb_mms_agent_pkg: https://mms.mongodb.com/download/agent/automation/mongodb-mms-automation-agent-manager_1.4.2.783-1_amd64.deb

View File

@ -1,6 +1,6 @@
#!/usr/bin/python
# (c) 2015, Sergei Antipov, 2GIS LLC
# (c) 2015-2016, Sergei Antipov, 2GIS LLC
#
# This file is part of Ansible
#
@ -23,6 +23,7 @@ module: mongodb_replication
short_description: Adds or removes a node from a MongoDB Replica Set.
description:
- Adds or removes host from a MongoDB replica set. Initialize replica set if it needed.
version_added: "2.1"
options:
login_user:
description:
@ -104,10 +105,10 @@ options:
default: present
choices: [ "present", "absent" ]
notes:
- Requires the pymongo Python package on the remote host, version 2.4.2+. This
- Requires the pymongo Python package on the remote host, version 3.0+. It
can be installed using pip or the OS package manager. @see http://api.mongodb.org/python/current/installation.html
requirements: [ "pymongo" ]
author: Sergei Antipov
author: "Sergei Antipov @UnderGreen"
'''
EXAMPLES = '''
@ -135,6 +136,7 @@ try:
from pymongo.errors import OperationFailure
from pymongo.errors import ConfigurationError
from pymongo.errors import AutoReconnect
from pymongo.errors import ServerSelectionTimeoutError
from pymongo import version as PyMongoVersion
from pymongo import MongoClient
from pymongo import MongoReplicaSetClient
@ -147,13 +149,11 @@ else:
# MongoDB module specific support methods.
#
def check_compatibility(module, client):
if LooseVersion(PyMongoVersion) <= LooseVersion('3.0'):
module.fail_json(msg='Note: you must use pymongo 3.0+')
srv_info = client.server_info()
if LooseVersion(srv_info['version']) >= LooseVersion('3.0') and LooseVersion(PyMongoVersion) <= LooseVersion('3.0'):
module.fail_json(msg=' (Note: you must use pymongo 3.0+ with MongoDB >= 3.0)')
elif LooseVersion(srv_info['version']) >= LooseVersion('2.6') and LooseVersion(PyMongoVersion) <= LooseVersion('2.7'):
module.fail_json(msg=' (Note: you must use pymongo 2.7.x-2.9.x with MongoDB 2.6)')
elif LooseVersion(PyMongoVersion) <= LooseVersion('2.5'):
module.fail_json(msg=' (Note: you must be on mongodb 2.4+ and pymongo 2.5+ to use the roles param)')
if LooseVersion(srv_info['version']) >= LooseVersion('3.2') and LooseVersion(PyMongoVersion) <= LooseVersion('3.2'):
module.fail_json(msg=' (Note: you must use pymongo 3.2+ with MongoDB >= 3.2)')
def check_members(state, module, client, host_name, host_port, host_type):
admin_db = client['admin']
@ -219,7 +219,7 @@ def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwa
cfg['members'].append(new_host)
admin_db.command('replSetReconfig', cfg)
return
except (OperationFailure, AutoReconnect) as e:
except (OperationFailure, AutoReconnect), e:
timeout = timeout - 5
if timeout <= 0:
module.fail_json(msg='reached timeout while waiting for rs.reconfig(): %s' % str(e))
@ -248,7 +248,7 @@ def remove_host(module, client, host_name, timeout=180):
else:
fail_msg = "couldn't find member with hostname: {0} in replica set members list".format(host_name)
module.fail_json(msg=fail_msg)
except (OperationFailure, AutoReconnect) as e:
except (OperationFailure, AutoReconnect), e:
timeout = timeout - 5
if timeout <= 0:
module.fail_json(msg='reached timeout while waiting for rs.reconfig(): %s' % str(e))
@ -292,6 +292,7 @@ def authenticate(client, login_user, login_password):
if login_user is not None and login_password is not None:
client.admin.authenticate(login_user, login_password)
# =========================================
# Module execution.
#
@ -307,7 +308,7 @@ def main():
host_name=dict(default='localhost'),
host_port=dict(default='27017'),
host_type=dict(default='replica', choices=['replica','arbiter']),
ssl=dict(default=False),
ssl=dict(default='false'),
build_indexes = dict(type='bool', default='yes'),
hidden = dict(type='bool', default='no'),
priority = dict(default='1.0'),
@ -338,17 +339,15 @@ def main():
if replica_set is None:
module.fail_json(msg='replica_set parameter is required')
else:
client = MongoReplicaSetClient(login_host, int(login_port), replicaSet=replica_set, ssl=ssl)
check_compatibility(module, client)
client = MongoClient(login_host, int(login_port), replicaSet=replica_set,
ssl=ssl, serverSelectionTimeoutMS=5000)
authenticate(client, login_user, login_password)
client['admin'].command('replSetGetStatus')
except ConnectionFailure, e:
module.fail_json(msg='unable to connect to database: %s' % str(e))
except ConfigurationError:
except ServerSelectionTimeoutError:
try:
client = MongoClient(login_host, int(login_port), ssl=ssl)
check_compatibility(module, client)
authenticate(client, login_user, login_password)
if state == 'present':
new_host = { '_id': 0, 'host': "{0}:{1}".format(host_name, host_port) }
@ -360,7 +359,10 @@ def main():
module.exit_json(changed=True, host_name=host_name, host_port=host_port, host_type=host_type)
except OperationFailure, e:
module.fail_json(msg='Unable to initiate replica set: %s' % str(e))
except ConnectionFailure, e:
module.fail_json(msg='unable to connect to database: %s' % str(e))
check_compatibility(module, client)
check_members(state, module, client, host_name, host_port, host_type)
if state == 'present':

View File

@ -82,8 +82,9 @@ options:
roles:
version_added: "1.3"
description:
- "The database user roles valid values are one or more of the following: read, 'readWrite', 'dbAdmin', 'userAdmin', 'clusterAdmin', 'readAnyDatabase', 'readWriteAnyDatabase', 'userAdminAnyDatabase', 'dbAdminAnyDatabase'"
- This param requires mongodb 2.4+ and pymongo 2.5+
- "The database user roles valid values could either be one or more of the following strings: 'read', 'readWrite', 'dbAdmin', 'userAdmin', 'clusterAdmin', 'readAnyDatabase', 'readWriteAnyDatabase', 'userAdminAnyDatabase', 'dbAdminAnyDatabase'"
- "Or the following dictionary '{ db: DATABASE_NAME, role: ROLE_NAME }'."
- "This param requires pymongo 2.5+. If it is a string, mongodb 2.4+ is also required. If it is a dictionary, mongo 2.6+ is required."
required: false
default: "readWrite"
state:
@ -124,7 +125,23 @@ EXAMPLES = '''
- mongodb_user: database=burgers name=joe password=12345 roles='readWriteAnyDatabase' state=present
# add a user to database in a replica set, the primary server is automatically discovered and written to
- mongodb_user: database=burgers name=bob replica_set=blecher password=12345 roles='readWriteAnyDatabase' state=present
- mongodb_user: database=burgers name=bob replica_set=belcher password=12345 roles='readWriteAnyDatabase' state=present
# add a user 'oplog_reader' with read only access to the 'local' database on the replica_set 'belcher'. This is usefull for oplog access (MONGO_OPLOG_URL).
# please notice the credentials must be added to the 'admin' database because the 'local' database is not syncronized and can't receive user credentials
# To login with such user, the connection string should be MONGO_OPLOG_URL="mongodb://oplog_reader:oplog_reader_password@server1,server2/local?authSource=admin"
# This syntax requires mongodb 2.6+ and pymongo 2.5+
- mongodb_user:
login_user: root
login_password: root_password
database: admin
user: oplog_reader
password: oplog_reader_password
state: present
replica_set: belcher
roles:
- { db: "local" , role: "read" }
'''
import ConfigParser
@ -150,23 +167,26 @@ else:
def check_compatibility(module, client):
srv_info = client.server_info()
if LooseVersion(srv_info['version']) >= LooseVersion('3.0') and LooseVersion(PyMongoVersion) <= LooseVersion('3.0'):
module.fail_json(msg=' (Note: you must use pymongo 3.0+ with MongoDB >= 3.0)')
if LooseVersion(srv_info['version']) >= LooseVersion('3.2') and LooseVersion(PyMongoVersion) <= LooseVersion('3.2'):
module.fail_json(msg=' (Note: you must use pymongo 3.2+ with MongoDB >= 3.2)')
elif LooseVersion(srv_info['version']) >= LooseVersion('3.0') and LooseVersion(PyMongoVersion) <= LooseVersion('2.8'):
module.fail_json(msg=' (Note: you must use pymongo 2.8+ with MongoDB 3.0)')
elif LooseVersion(srv_info['version']) >= LooseVersion('2.6') and LooseVersion(PyMongoVersion) <= LooseVersion('2.7'):
module.fail_json(msg=' (Note: you must use pymongo 2.7.x-2.9.x with MongoDB 2.6)')
module.fail_json(msg=' (Note: you must use pymongo 2.7+ with MongoDB 2.6)')
elif LooseVersion(PyMongoVersion) <= LooseVersion('2.5'):
module.fail_json(msg=' (Note: you must be on mongodb 2.4+ and pymongo 2.5+ to use the roles param)')
def user_find(client, user):
def user_find(client, user, db_name):
for mongo_user in client["admin"].system.users.find():
if mongo_user['user'] == user:
if mongo_user['user'] == user and mongo_user['db'] == db_name:
return mongo_user
return False
def user_add(module, client, db_name, user, password, roles):
#pymono's user_add is a _create_or_update_user so we won't know if it was changed or updated
#pymongo's user_add is a _create_or_update_user so we won't know if it was changed or updated
#without reproducing a lot of the logic in database.py of pymongo
db = client[db_name]
if roles is None:
db.add_user(user, password, False)
else:
@ -177,8 +197,10 @@ def user_add(module, client, db_name, user, password, roles):
module.fail_json(msg=err_msg)
def user_remove(module, client, db_name, user):
exists = user_find(client, user)
exists = user_find(client, user, db_name)
if exists:
if module.check_mode:
module.exit_json(changed=True, user=user)
db = client[db_name]
db.remove_user(user)
else:
@ -199,6 +221,44 @@ def load_mongocnf():
return creds
def check_if_roles_changed(uinfo, roles, db_name):
# We must be aware of users which can read the oplog on a replicaset
# Such users must have access to the local DB, but since this DB does not store users credentials
# and is not synchronized among replica sets, the user must be stored on the admin db
# Therefore their structure is the following :
# {
# "_id" : "admin.oplog_reader",
# "user" : "oplog_reader",
# "db" : "admin", # <-- admin DB
# "roles" : [
# {
# "role" : "read",
# "db" : "local" # <-- local DB
# }
# ]
# }
def make_sure_roles_are_a_list_of_dict(roles, db_name):
output = list()
for role in roles:
if isinstance(role, basestring):
new_role = { "role": role, "db": db_name }
output.append(new_role)
else:
output.append(role)
return output
roles_as_list_of_dict = make_sure_roles_are_a_list_of_dict(roles, db_name)
uinfo_roles = uinfo.get('roles', [])
if sorted(roles_as_list_of_dict) == sorted(uinfo_roles):
return False
return True
# =========================================
# Module execution.
#
@ -215,11 +275,12 @@ def main():
database=dict(required=True, aliases=['db']),
name=dict(required=True, aliases=['user']),
password=dict(aliases=['pass']),
ssl=dict(default=False),
ssl=dict(default=False, type='bool'),
roles=dict(default=None, type='list'),
state=dict(default='present', choices=['absent', 'present']),
update_password=dict(default="always", choices=["always", "on_create"]),
)
),
supports_check_mode=True
)
if not pymongo_found:
@ -230,7 +291,7 @@ def main():
login_host = module.params['login_host']
login_port = module.params['login_port']
login_database = module.params['login_database']
replica_set = module.params['replica_set']
db_name = module.params['database']
user = module.params['name']
@ -265,19 +326,30 @@ def main():
module.fail_json(msg='unable to connect to database: %s' % str(e))
check_compatibility(module, client)
if state == 'present':
if password is None and update_password == 'always':
module.fail_json(msg='password parameter required when adding a user unless update_password is set to on_create')
if update_password != 'always' and user_find(client, user):
uinfo = user_find(client, user, db_name)
if update_password != 'always' and uinfo:
password = None
if not check_if_roles_changed(uinfo, roles, db_name):
module.exit_json(changed=False, user=user)
if module.check_mode:
module.exit_json(changed=True, user=user)
try:
user_add(module, client, db_name, user, password, roles)
except OperationFailure, e:
module.fail_json(msg='Unable to add or update user: %s' % str(e))
# Here we can check password change if mongo provide a query for that : https://jira.mongodb.org/browse/SERVER-22848
#newuinfo = user_find(client, user, db_name)
#if uinfo['role'] == newuinfo['role'] and CheckPasswordHere:
# module.exit_json(changed=False, user=user)
elif state == 'absent':
try:
user_remove(module, client, db_name, user)

View File

@ -7,7 +7,7 @@
when: mongodb_manage_service
- name: wait MongoDB port is listening
wait_for: host=127.0.0.1 port="{{ mongodb_conf_port }}" delay=5 state=started
wait_for: host=127.0.0.1 port="{{ mongodb_net_port }}" delay=5 state=started
- name: get pid of mongodb for non daemon mode
shell: "pidof mongod"
@ -24,8 +24,9 @@
database: admin
name: "{{ item.name }}"
password: "{{ item.password }}"
update_password: "{{ mongodb_user_update_password }}"
roles: "{{ item.roles }}"
login_port: "{{ mongodb_conf_port }}"
login_port: "{{ mongodb_net_port }}"
with_items:
- {
name: "{{ mongodb_user_admin_name }}",
@ -38,8 +39,9 @@
database: admin
name: "{{ item.name }}"
password: "{{ item.password }}"
update_password: "{{ mongodb_user_update_password }}"
roles: "{{ item.roles }}"
login_port: "{{ mongodb_conf_port }}"
login_port: "{{ mongodb_net_port }}"
with_items:
- {
name: "{{ mongodb_root_admin_name }}",
@ -47,19 +49,6 @@
roles: "root"
}
- name: create normal users
mongodb_user:
database: "{{ item.database }}"
name: "{{ item.name }}"
password: "{{ item.password }}"
roles: "{{ item.roles }}"
login_user: "{{ mongodb_user_admin_name }}"
login_password: "{{ mongodb_user_admin_password }}"
login_port: "{{ mongodb_conf_port }}"
with_items:
- "{{ mongodb_users }}"
when: mongodb_users is defined and mongodb_users
- name: Move back mongod.conf
template: src=mongod.conf.j2 dest=/etc/mongod.conf owner=root group=root mode=0644
@ -68,7 +57,7 @@
when: mongodb_manage_service
- name: wait MongoDB port is listening
wait_for: host="{{ mongodb_conf_bind_ip }}" port="{{ mongodb_conf_port }}" delay=5 state=started
wait_for: host="{{ mongodb_net_bindip }}" port="{{ mongodb_net_port }}" delay=5 state=started
- name: stop mongodb if was not started
shell: "kill {{ pidof_mongod.stdout }}"

View File

@ -2,12 +2,12 @@
- name: Create keyFile
copy:
dest: "{{ mongodb_conf_keyFile }}"
dest: "{{ mongodb_security_keyfile }}"
content: "{{ mongodb_keyfile_content }}"
owner: "{{ mongodb_user }}"
group: "root"
mode: 0600
when: mongodb_conf_replSet != ''
when: mongodb_replication_replset and mongodb_replication_replset != ''
- name: Configure log rotation
template: src=logrotate.conf.j2 dest=/etc/logrotate.d/mongodb.conf
@ -22,18 +22,18 @@
when: mongodb_uid
- name: reset mongodb folder and subfolders with new uid
file: path={{ mongodb_conf_dbpath }} owner=mongodb group=mongodb follow=yes recurse=yes state=directory
file: path={{ mongodb_storage_dbpath }} owner=mongodb group=mongodb follow=yes recurse=yes state=directory
when: mongodb_uid
- name: Create log dir if missing
file: state=directory recurse=yes dest={{ mongodb_conf_logpath|dirname }} owner={{ mongodb_user }} group={{mongodb_user}} mode=0755
file: state=directory recurse=yes dest={{ mongodb_systemlog_path|dirname }} owner={{ mongodb_user }} group={{mongodb_user}} mode=0755
- name: Check than logfile exists
stat: path={{ mongodb_conf_logpath }}
stat: path={{ mongodb_systemlog_path }}
register: logfile_stat
- name: Create log if missing
file: state=touch dest={{ mongodb_conf_logpath }} owner={{ mongodb_user }} group={{mongodb_user}} mode=0755
file: state=touch dest={{ mongodb_systemlog_path }} owner={{ mongodb_user }} group={{mongodb_user}} mode=0755
when: logfile_stat is defined and not logfile_stat.stat.exists
- name: Configure mongodb
@ -70,10 +70,10 @@
- name: Set fact about wait_for host address
set_fact:
wait_for_host: 127.0.0.1
when: mongodb_conf_bind_ip == "0.0.0.0"
when: mongodb_net_bindip == "0.0.0.0"
- name: Wait when mongodb is started
wait_for:
host: "{{ wait_for_host | default(mongodb_conf_bind_ip) }}"
port: "{{ mongodb_conf_port }}"
host: "{{ wait_for_host | default(mongodb_net_bindip) }}"
port: "{{ mongodb_net_port }}"
timeout: 120

View File

@ -20,17 +20,23 @@
when: "'systemd' in systemd.stdout"
- name: Add APT key
apt_key: url="{{mongodb_apt_key_url}}" id="{{mongodb_apt_key_id}}"
apt_key:
keyserver: "{{mongodb_apt_keyserver}}"
id: "{{mongodb_apt_key_id}}"
when: mongodb_package == 'mongodb-org'
- name: Fail when used wrong mongodb_version variable
fail:
msg: "mongodb_version variable should be '2.6', '3.0' or '3.2'"
when: mongodb_version is not defined or mongodb_repository[item] is not defined
when: (mongodb_package == 'mongodb-org' and
(mongodb_version is not defined
or mongodb_repository[item] is not defined))
with_items: "{{ mongodb_version[0:3] }}"
- name: Add APT repository
apt_repository: repo="{{ mongodb_repository[item] }}" update_cache=yes
with_items: "{{ mongodb_version[0:3] }}"
when: mongodb_package == 'mongodb-org'
- name: Install MongoDB package
apt: name={{item}} state=present
@ -39,7 +45,7 @@
- numactl
- name: Ensure dbpath directory
file: path={{mongodb_conf_dbpath}} state=directory owner=mongodb recurse=yes
file: path={{mongodb_storage_dbpath}} state=directory owner=mongodb recurse=yes
- name: reload systemd
shell: systemctl daemon-reload
@ -58,5 +64,5 @@
when: mongodb_pymongo_from_pip
- name: Install PyMongo from PIP
pip: name=pymongo version="{{ mongodb_pymongo_pip_version }}" state=present
pip: name=pymongo state=latest
when: mongodb_pymongo_from_pip

View File

@ -1,25 +1,65 @@
---
- include: install.deb.yml
- name: Include installation on Debian-based OS
include: install.deb.yml
when: ansible_os_family == 'Debian'
tags: [mongodb]
- include: configure.yml
- name: Include configuration.yml
include: configure.yml
tags: [mongodb]
- include: replication_init_auth.yml
when: ( mongodb_conf_replSet != "" and mongodb_conf_auth
- name: Include replication and auth configuration
include: replication_init_auth.yml
when: ( mongodb_replication_replset
and mongodb_replication_replset != ''
and mongodb_security_authorization == 'enabled'
and mongodb_master is defined and mongodb_master )
tags: [mongodb]
- include: replication.yml
when: mongodb_conf_replSet != ""
- name: Include replication configuration
include: replication.yml
when: mongodb_replication_replset and mongodb_replication_replset != ''
tags: [mongodb]
- include: auth_initialization.yml
when: mongodb_conf_auth and not mongodb_conf_replSet
- name: Check user admin is exists
command: >
mongo --quiet -u {{ mongodb_user_admin_name }} \
-p {{ mongodb_user_admin_password }} --eval 'db.version()' admin
register: mongodb_user_admin_check
changed_when: false
ignore_errors: true
when: ( mongodb_security_authorization == 'enabled'
and (not mongodb_replication_replset
or mongodb_replication_replset == '') )
tags: [mongodb]
- include: mms-agent.yml
- name: Include authorization configuration
include: auth_initialization.yml
when: ( mongodb_security_authorization == 'enabled'
and (not mongodb_replication_replset
or mongodb_replication_replset == '')
and mongodb_user_admin_check.rc != 0 )
tags: [mongodb]
- name: create normal users
mongodb_user:
database: "{{ item.database }}"
name: "{{ item.name }}"
password: "{{ item.password }}"
update_password: "{{ mongodb_user_update_password }}"
roles: "{{ item.roles }}"
login_user: "{{ mongodb_user_admin_name }}"
login_password: "{{ mongodb_user_admin_password }}"
login_port: "{{ mongodb_net_port }}"
login_database: "admin"
with_items:
- "{{ mongodb_users | default([]) }}"
when: mongodb_security_authorization == 'enabled'
tags: [mongodb]
- name: Include MMS Agent configuration
include: mms-agent.yml
when: mongodb_mms_api_key != ""
tags: [mongodb]

View File

@ -1,11 +1,11 @@
---
- name: Install MMS agent pt. 1
get_url: url={{mongodb_mms_agent_pkg}} dest={{mongodb_conf_dbpath}}/mms-agent.deb
get_url: url={{mongodb_mms_agent_pkg}} dest={{mongodb_storage_dbpath}}/mms-agent.deb
register: mongodb_mms_agent_loaded
- name: Install MMS agent pt. 2
apt: deb={{mongodb_conf_dbpath}}/mms-agent.deb
apt: deb={{mongodb_storage_dbpath}}/mms-agent.deb
when: mongodb_mms_agent_loaded.changed
- name: Configure the MMS agent pt. 1

View File

@ -6,13 +6,13 @@
login_port: "{{ mongodb_login_port|default(27017) }}"
login_user: "{{ mongodb_root_admin_name }}"
login_password: "{{ mongodb_root_admin_password }}"
replica_set: "{{ mongodb_conf_replSet }}"
replica_set: "{{ mongodb_replication_replset }}"
host_name: "{{ item.host_name }}"
host_port: "{{ item.host_port|default(27017) }}"
host_type: "{{ item.host_type|default('replica') }}"
hidden: "{{ item.hidden|default(false) }}"
priority: "{{ item.priority|default(1.0) }}"
when: mongodb_conf_auth and mongodb_replication_params is defined
when: mongodb_security_authorization == 'enabled'
with_items:
- "{{ mongodb_replication_params|default([]) }}"
@ -20,12 +20,12 @@
mongodb_replication:
login_host: "{{ mongodb_login_host|default('localhost') }}"
login_port: "{{ mongodb_login_port|default(27017) }}"
replica_set: "{{ mongodb_conf_replSet }}"
replica_set: "{{ mongodb_replication_replset }}"
host_name: "{{ item.host_name }}"
host_port: "{{ item.host_port|default(27017) }}"
host_type: "{{ item.host_type|default('replica') }}"
hidden: "{{ item.hidden|default(false) }}"
priority: "{{ item.priority|default(1.0) }}"
when: not mongodb_conf_auth and mongodb_replication_params is defined
when: mongodb_security_authorization == 'disabled'
with_items:
- "{{ mongodb_replication_params|default([]) }}"

View File

@ -1,17 +1,16 @@
---
- name: Replication configuration
- name: Replication configuration | 1st Pt.
mongodb_replication:
login_host: "{{ mongodb_login_host|default('localhost') }}"
login_port: "{{ mongodb_conf_port|default(27017) }}"
login_port: "{{ mongodb_net_port|default(27017) }}"
login_user: "{{ mongodb_root_admin_name }}"
login_password: "{{ mongodb_root_admin_password }}"
replica_set: "{{ mongodb_conf_replSet }}"
replica_set: "{{ mongodb_replication_replset }}"
host_name: "{{ item.host_name }}"
host_port: "{{ item.host_port|default(27017) }}"
host_type: "{{ item.host_type|default('replica') }}"
hidden: "{{ item.hidden|default(false) }}"
priority: "{{ item.priority|default(1.0) }}"
when: mongodb_replication_params is defined
with_items:
- "{{ mongodb_replication_params|default([]) }}"
register: mongodb_replica_init
@ -20,19 +19,18 @@
- include: auth_initialization.yml
when: mongodb_replica_init|failed
- name: Replication configuration
- name: Replication configuration | 2nd Pt
mongodb_replication:
login_host: "{{ mongodb_login_host|default('localhost') }}"
login_port: "{{ mongodb_conf_port|default(27017) }}"
login_port: "{{ mongodb_net_port|default(27017) }}"
login_user: "{{ mongodb_root_admin_name }}"
login_password: "{{ mongodb_root_admin_password }}"
replica_set: "{{ mongodb_conf_replSet }}"
replica_set: "{{ mongodb_replication_replset }}"
host_name: "{{ item.host_name }}"
host_port: "{{ item.host_port|default(27017) }}"
host_type: "{{ item.host_type|default('replica') }}"
hidden: "{{ item.hidden|default(false) }}"
priority: "{{ item.priority|default(1.0) }}"
when: mongodb_replication_params is defined
when: mongodb_replica_init|failed
with_items:
- "{{ mongodb_replication_params|default([]) }}"
when: mongodb_replica_init|failed

View File

@ -1,7 +1,7 @@
# This file was generated by Ansible for {{ ansible_fqdn }}
# Do NOT modify this file by hand!
{{ mongodb_conf_logpath }} {
{{ mongodb_systemlog_path }} {
{% for option in mongodb_logrotate_options %}
{{ option }}
{% endfor %}

View File

@ -1,33 +1,55 @@
# {{ ansible_managed }}
auth = {{ mongodb_conf_auth|to_nice_json }}
bind_ip = {{ mongodb_conf_bind_ip }}
cpu = {{ mongodb_conf_cpu|to_nice_json }}
dbpath = {{ mongodb_conf_dbpath }}
fork = {{ mongodb_conf_fork|to_nice_json }}
httpinterface = {{ mongodb_conf_httpinterface|to_nice_json }}
ipv6 = {{ mongodb_conf_ipv6|to_nice_json }}
journal = {{ mongodb_conf_journal|to_nice_json }}
logappend = {{ mongodb_conf_logappend|to_nice_json }}
logpath = {{ mongodb_conf_logpath }}
maxConns = {{ mongodb_conf_maxConns }}
noprealloc = {{ mongodb_conf_noprealloc|to_nice_json }}
noscripting = {{ mongodb_conf_noscripting|to_nice_json }}
notablescan = {{ mongodb_conf_notablescan|to_nice_json }}
port = {{ mongodb_conf_port }}
quota = {{ mongodb_conf_quota|to_nice_json }}
{% if mongodb_conf_quota %}
quotaFiles = {{ mongodb_conf_quotaFiles }}
{% endif %}
syslog = {{ mongodb_conf_syslog|to_nice_json }}
smallfiles = {{ mongodb_conf_smallfiles|to_nice_json }}
net:
bindIp: {{ mongodb_net_bindip }}
http:
enabled: {{ mongodb_net_http_enabled | to_nice_json }}
ipv6: {{ mongodb_net_ipv6 | to_nice_json }}
maxIncomingConnections: {{ mongodb_net_maxconns }}
port: {{ mongodb_net_port }}
{% if mongodb_conf_replSet %}
# Replica set options:
replSet = {{ mongodb_conf_replSet }}
replIndexPrefetch = {{ mongodb_conf_replIndexPrefetch }}
oplogSize = {{ mongodb_conf_oplogSize }}
{% endif %}
{% if mongodb_conf_replSet and mongodb_conf_auth %}
keyFile = {{ mongodb_conf_keyFile }}
processManagement:
fork: {{ mongodb_processmanagement_fork | to_nice_json}}
{% if mongodb_replication_replset -%}
replication:
oplogSizeMB: {{ mongodb_replication_oplogsize | int }}
replSetName: {{ mongodb_replication_replset }}
{% if mongodb_storage_engine == 'mmapv1' -%}
secondaryIndexPrefetch: {{ mongodb_replication_replindexprefetch }}
{% endif %}
{% endif %}
security:
authorization: {{ mongodb_security_authorization }}
{% if mongodb_replication_replset and mongodb_security_authorization == 'enabled' -%}
keyFile: {{ mongodb_security_keyfile }}
{% endif %}
storage:
dbPath: {{ mongodb_storage_dbpath }}
{% if mongodb_version[0:3]|float >= 3.0 -%}
engine: {{ mongodb_storage_engine }}
{% endif -%}
journal:
enabled: {{ mongodb_storage_journal_enabled | to_nice_json }}
{% if mongodb_storage_engine == 'mmapv1' -%}
mmapv1:
quota:
enforced: {{ mongodb_storage_quota_enforced | to_nice_json }}
maxFilesPerDB: {{ mongodb_storage_quota_maxfiles }}
smallFiles: {{ mongodb_storage_smallfiles | to_nice_json }}
{% endif -%}
{% if mongodb_version[0:3] == '2.6' -%}
quota:
enforced: {{ mongodb_storage_quota_enforced | to_nice_json }}
maxFilesPerDB: {{ mongodb_storage_quota_maxfiles }}
preallocDataFiles: {{ mongodb_storage_prealloc | to_nice_json }}
smallFiles: {{ mongodb_storage_smallfiles | to_nice_json }}
{% endif %}
systemLog:
destination: {{ mongodb_systemlog_destination }}
logAppend: {{ mongodb_systemlog_logappend | to_nice_json }}
path: {{ mongodb_systemlog_path }}

View File

@ -1,23 +1,42 @@
# {{ ansible_managed }}
noauth = true
bind_ip = 127.0.0.1
cpu = {{ mongodb_conf_cpu|to_nice_json }}
dbpath = {{ mongodb_conf_dbpath }}
fork = {{ mongodb_conf_fork|to_nice_json }}
httpinterface = {{ mongodb_conf_httpinterface|to_nice_json }}
ipv6 = {{ mongodb_conf_ipv6|to_nice_json }}
journal = {{ mongodb_conf_journal|to_nice_json }}
logappend = {{ mongodb_conf_logappend|to_nice_json }}
logpath = {{ mongodb_conf_logpath }}
maxConns = {{ mongodb_conf_maxConns }}
noprealloc = {{ mongodb_conf_noprealloc|to_nice_json }}
noscripting = {{ mongodb_conf_noscripting|to_nice_json }}
notablescan = {{ mongodb_conf_notablescan|to_nice_json }}
port = {{ mongodb_conf_port }}
quota = {{ mongodb_conf_quota|to_nice_json }}
{% if mongodb_conf_quota %}
quotaFiles = {{ mongodb_conf_quotaFiles }}
{% endif %}
syslog = {{ mongodb_conf_syslog|to_nice_json }}
smallfiles = {{ mongodb_conf_smallfiles|to_nice_json }}
net:
bindIp: '127.0.0.1'
http:
enabled: {{ mongodb_net_http_enabled | to_nice_json }}
ipv6: {{ mongodb_net_ipv6 | to_nice_json }}
maxIncomingConnections: {{ mongodb_net_maxconns }}
port: {{ mongodb_net_port }}
processManagement:
fork: {{ mongodb_processmanagement_fork | to_nice_json }}
security:
authorization: 'disabled'
storage:
dbPath: {{ mongodb_storage_dbpath }}
{% if mongodb_version[0:3]|float >= 3.0 -%}
engine: {{ mongodb_storage_engine }}
{% endif -%}
journal:
enabled: {{ mongodb_storage_journal_enabled | to_nice_json }}
{% if mongodb_storage_engine == 'mmapv1' -%}
mmapv1:
quota:
enforced: {{ mongodb_storage_quota_enforced | to_nice_json }}
maxFilesPerDB: {{ mongodb_storage_quota_maxfiles }}
smallFiles: {{ mongodb_storage_smallfiles | to_nice_json }}
{% endif -%}
{% if mongodb_version[0:3] == '2.6' -%}
quota:
enforced: {{ mongodb_storage_quota_enforced | to_nice_json }}
maxFilesPerDB: {{ mongodb_storage_quota_maxfiles }}
preallocDataFiles: {{ mongodb_storage_prealloc | to_nice_json }}
smallFiles: {{ mongodb_storage_smallfiles | to_nice_json }}
{% endif %}
systemLog:
destination: {{ mongodb_systemlog_destination }}
logAppend: {{ mongodb_systemlog_logappend | to_nice_json }}
path: {{ mongodb_systemlog_path }}

15
tests/group_vars/all.yml Normal file
View File

@ -0,0 +1,15 @@
---
image_name: "ubuntu-upstart:14.04"
mongodb_version: "3.0"
mongodb_storage_dbpath: /var/lib/mongodb
mongodb_net_bindip: 0.0.0.0
mongodb_login_host: "{{ hostvars[groups['mongo_master'][0]].ansible_default_ipv4.address }}"
mongodb_users:
- {
name: testUser,
password: passw0rd,
roles: readWrite,
database: admin
}

View File

@ -0,0 +1,5 @@
---
mongodb_master: true
mongodb_replication_params:
- { host_name: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}" }

View File

@ -0,0 +1,4 @@
---
mongodb_replication_params:
- { host_name: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}" }

View File

@ -0,0 +1,9 @@
---
mongodb_net_port: 30000
mongodb_storage_journal_enabled: false
mongodb_storage_smallfiles: true
mongodb_storage_prealloc: false
mongodb_replication_params:
- host_name: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}"
host_port: "{{ mongodb_net_port }}"
host_type: arbiter

13
tests/hosts Normal file
View File

@ -0,0 +1,13 @@
[local]
localhost ansible_connection=local ansible_python_interpreter=python2
[mongo_master]
mongo1 ansible_connection=docker ansible_python_interpreter=python2
[mongo_slave]
mongo2 ansible_connection=docker ansible_python_interpreter=python2
mongo3 ansible_connection=docker ansible_python_interpreter=python2
[mongo:children]
mongo_master
mongo_slave

39
tests/site.yml Normal file
View File

@ -0,0 +1,39 @@
# test.yml
---
- hosts: localhost
become: no
gather_facts: no
tasks:
- name: Run MongoDB cluster in Docker
docker:
name: "{{ item }}"
image: "{{ image_name }}"
command: "/sbin/init"
state: started
with_items:
- mongo1
- mongo2
- mongo3
- hosts: "{{ target }}"
become: no
gather_facts: yes
roles:
- role: greendayonfire.mongodb
when: "'mongo' in group_names"
- hosts: localhost
become: no
gather_facts: no
tasks:
- name: Run MongoDB cluster in Docker
docker:
name: "{{ item }}"
image: "{{ image_name }}"
command: "/sbin/init"
state: absent
with_items:
- mongo1
- mongo2
- mongo3

View File

@ -1,3 +1,6 @@
---
mongodb_repository: deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen
mongodb_repository:
"2.6": "deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen"
"3.0": "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.0 main"
"3.2": "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.2 main"