Skip to content

Commit 6633edc

Browse files
author
Sergei Antipov
committed
Improved mongodb_replication module
1 parent ecff84d commit 6633edc

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

library/mongodb_replication.py

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,38 @@
6868
description:
6969
- Whether to use an SSL connection when connecting to the database
7070
default: False
71+
build_indexes:
72+
description:
73+
- Determines whether the mongod builds indexes on this member.
74+
required: false
75+
default: true
76+
hidden:
77+
description:
78+
- When this value is true, the replica set hides this instance,
79+
and does not include the member in the output of db.isMaster()
80+
or isMaster
81+
required: false
82+
default: false
83+
priority:
84+
description:
85+
- A number that indicates the relative eligibility of a member
86+
to become a primary
87+
required: false
88+
default: 1.0
89+
slave_delay:
90+
description:
91+
- The number of seconds behind the primary that this replica set
92+
member should lag
93+
required: false
94+
default: 0
95+
votes:
96+
description:
97+
- The number of votes a server will cast in a replica set election
98+
default: 1
7199
state:
72100
state:
73101
description:
74-
- The database user state
102+
- The replica set member state
75103
required: false
76104
default: present
77105
choices: [ "present", "absent" ]
@@ -146,7 +174,7 @@ def check_members(state, module, client, host_name, host_port, host_type):
146174
if "{0}:{1}".format(host_name, host_port) not in member['host'] and member['arbiterOnly']:
147175
module.exit_json(changed=False, host_name=host_name, host_port=host_port, host_type=host_type)
148176

149-
def add_host(module, client, host_name, host_port, host_type, timeout=180):
177+
def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwargs):
150178
while True:
151179
try:
152180
admin_db = client['admin']
@@ -165,6 +193,21 @@ def add_host(module, client, host_name, host_port, host_type, timeout=180):
165193
if host_type == 'arbiter':
166194
new_host['arbiterOnly'] = True
167195

196+
if not kwargs['build_indexes']:
197+
new_host['buildIndexes'] = False
198+
199+
if kwargs['hidden']:
200+
new_host['hidden'] = True
201+
202+
if kwargs['priority'] != 1.0:
203+
new_host['priority'] = kwargs['priority']
204+
205+
if kwargs['slave_delay'] != 0:
206+
new_host['slaveDelay'] = kwargs['slave_delay']
207+
208+
if kwargs['votes'] != 1:
209+
new_host['votes'] = kwargs['votes']
210+
168211
cfg['members'].append(new_host)
169212
admin_db.command('replSetReconfig', cfg)
170213
return
@@ -230,18 +273,6 @@ def wait_for_ok_and_master(module, client, timeout = 60):
230273

231274
time.sleep(1)
232275

233-
def reconfigure(module,client, rs_config, timeout = 180):
234-
while True:
235-
try:
236-
client['admin'].command('replSetReconfig', rs_config)
237-
return
238-
except (OperationFailure, AutoReconnect) as e:
239-
print e
240-
timeout = timeout - 5
241-
if timeout <= 0:
242-
module.fail_json(msg='reached timeout while waiting for rs.reconfig()')
243-
time.sleep(5)
244-
245276
def authenticate(client, login_user, login_password):
246277
if login_user is None and login_password is None:
247278
mongocnf_creds = load_mongocnf()
@@ -269,6 +300,11 @@ def main():
269300
host_port=dict(default='27017'),
270301
host_type=dict(default='replica', choices=['replica','arbiter']),
271302
ssl=dict(default=False),
303+
build_indexes = dict(type='bool', choices=BOOLEANS, default='yes'),
304+
hidden = dict(type='bool', choices=BOOLEANS, default='no'),
305+
priority = dict(default='1.0'),
306+
slave_delay = dict(type='int', default='0'),
307+
votes = dict(type='int', default='1'),
272308
state=dict(default='present', choices=['absent', 'present']),
273309
)
274310
)
@@ -320,7 +356,12 @@ def main():
320356

321357
try:
322358
if not replica_set_created:
323-
add_host(module, client, host_name, host_port, host_type)
359+
add_host(module, client, host_name, host_port, host_type,
360+
build_indexes = module.params['build_indexes'],
361+
hidden = module.params['hidden'],
362+
priority = float(module.params['priority']),
363+
slave_delay = module.params['slave_delay'],
364+
votes = module.params['votes'])
324365
except OperationFailure, e:
325366
module.fail_json(msg='Unable to add new member to replica set: %s' % str(e))
326367

0 commit comments

Comments
 (0)