From 6633edc0c5ec4bf0bff307cce5d243b49ef71e1d Mon Sep 17 00:00:00 2001 From: Sergei Antipov Date: Tue, 24 Feb 2015 17:55:21 +0600 Subject: [PATCH] Improved mongodb_replication module --- library/mongodb_replication.py | 71 +++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/library/mongodb_replication.py b/library/mongodb_replication.py index dea970d..f596adf 100644 --- a/library/mongodb_replication.py +++ b/library/mongodb_replication.py @@ -68,10 +68,38 @@ options: description: - Whether to use an SSL connection when connecting to the database default: False + build_indexes: + description: + - Determines whether the mongod builds indexes on this member. + required: false + default: true + hidden: + description: + - When this value is true, the replica set hides this instance, + and does not include the member in the output of db.isMaster() + or isMaster + required: false + default: false + priority: + description: + - A number that indicates the relative eligibility of a member + to become a primary + required: false + default: 1.0 + slave_delay: + description: + - The number of seconds behind the primary that this replica set + member should lag + required: false + default: 0 + votes: + description: + - The number of votes a server will cast in a replica set election + default: 1 state: state: description: - - The database user state + - The replica set member state required: false default: present choices: [ "present", "absent" ] @@ -146,7 +174,7 @@ def check_members(state, module, client, host_name, host_port, host_type): if "{0}:{1}".format(host_name, host_port) not in member['host'] and member['arbiterOnly']: module.exit_json(changed=False, host_name=host_name, host_port=host_port, host_type=host_type) -def add_host(module, client, host_name, host_port, host_type, timeout=180): +def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwargs): while True: try: admin_db = client['admin'] @@ -165,6 +193,21 @@ def add_host(module, client, host_name, host_port, host_type, timeout=180): if host_type == 'arbiter': new_host['arbiterOnly'] = True + if not kwargs['build_indexes']: + new_host['buildIndexes'] = False + + if kwargs['hidden']: + new_host['hidden'] = True + + if kwargs['priority'] != 1.0: + new_host['priority'] = kwargs['priority'] + + if kwargs['slave_delay'] != 0: + new_host['slaveDelay'] = kwargs['slave_delay'] + + if kwargs['votes'] != 1: + new_host['votes'] = kwargs['votes'] + cfg['members'].append(new_host) admin_db.command('replSetReconfig', cfg) return @@ -230,18 +273,6 @@ def wait_for_ok_and_master(module, client, timeout = 60): time.sleep(1) -def reconfigure(module,client, rs_config, timeout = 180): - while True: - try: - client['admin'].command('replSetReconfig', rs_config) - return - except (OperationFailure, AutoReconnect) as e: - print e - timeout = timeout - 5 - if timeout <= 0: - module.fail_json(msg='reached timeout while waiting for rs.reconfig()') - time.sleep(5) - def authenticate(client, login_user, login_password): if login_user is None and login_password is None: mongocnf_creds = load_mongocnf() @@ -269,6 +300,11 @@ def main(): host_port=dict(default='27017'), host_type=dict(default='replica', choices=['replica','arbiter']), ssl=dict(default=False), + build_indexes = dict(type='bool', choices=BOOLEANS, default='yes'), + hidden = dict(type='bool', choices=BOOLEANS, default='no'), + priority = dict(default='1.0'), + slave_delay = dict(type='int', default='0'), + votes = dict(type='int', default='1'), state=dict(default='present', choices=['absent', 'present']), ) ) @@ -320,7 +356,12 @@ def main(): try: if not replica_set_created: - add_host(module, client, host_name, host_port, host_type) + add_host(module, client, host_name, host_port, host_type, + build_indexes = module.params['build_indexes'], + hidden = module.params['hidden'], + priority = float(module.params['priority']), + slave_delay = module.params['slave_delay'], + votes = module.params['votes']) except OperationFailure, e: module.fail_json(msg='Unable to add new member to replica set: %s' % str(e))