Improved mongodb_replication module

This commit is contained in:
Sergei Antipov 2015-02-24 17:55:21 +06:00
parent ecff84de62
commit 6633edc0c5
1 changed files with 56 additions and 15 deletions

View File

@ -68,10 +68,38 @@ options:
description: description:
- Whether to use an SSL connection when connecting to the database - Whether to use an SSL connection when connecting to the database
default: False 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:
state: state:
description: description:
- The database user state - The replica set member state
required: false required: false
default: present default: present
choices: [ "present", "absent" ] 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']: 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) 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: while True:
try: try:
admin_db = client['admin'] 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': if host_type == 'arbiter':
new_host['arbiterOnly'] = True 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) cfg['members'].append(new_host)
admin_db.command('replSetReconfig', cfg) admin_db.command('replSetReconfig', cfg)
return return
@ -230,18 +273,6 @@ def wait_for_ok_and_master(module, client, timeout = 60):
time.sleep(1) 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): def authenticate(client, login_user, login_password):
if login_user is None and login_password is None: if login_user is None and login_password is None:
mongocnf_creds = load_mongocnf() mongocnf_creds = load_mongocnf()
@ -269,6 +300,11 @@ def main():
host_port=dict(default='27017'), host_port=dict(default='27017'),
host_type=dict(default='replica', choices=['replica','arbiter']), host_type=dict(default='replica', choices=['replica','arbiter']),
ssl=dict(default=False), 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']), state=dict(default='present', choices=['absent', 'present']),
) )
) )
@ -320,7 +356,12 @@ def main():
try: try:
if not replica_set_created: 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: except OperationFailure, e:
module.fail_json(msg='Unable to add new member to replica set: %s' % str(e)) module.fail_json(msg='Unable to add new member to replica set: %s' % str(e))