1
1
#!/usr/bin/python
2
2
3
- # (c) 2015-2016 , Sergei Antipov, 2GIS LLC
3
+ # (c) 2015-2018 , Sergei Antipov, 2GIS LLC
4
4
#
5
5
# This file is part of Ansible
6
6
#
23
23
short_description: Adds or removes a node from a MongoDB Replica Set.
24
24
description:
25
25
- Adds or removes host from a MongoDB replica set. Initialize replica set if it needed.
26
- version_added: "2.2 "
26
+ version_added: "2.4 "
27
27
options:
28
28
login_user:
29
29
description:
105
105
default: present
106
106
choices: [ "present", "absent" ]
107
107
notes:
108
- - Requires the pymongo Python package on the remote host, version 3.0 +. It
108
+ - Requires the pymongo Python package on the remote host, version 3.2 +. It
109
109
can be installed using pip or the OS package manager. @see http://api.mongodb.org/python/current/installation.html
110
110
requirements: [ "pymongo" ]
111
111
author: "Sergei Antipov @UnderGreen"
166
166
# MongoDB module specific support methods.
167
167
#
168
168
def check_compatibility (module , client ):
169
- if LooseVersion (PyMongoVersion ) <= LooseVersion ('3.0' ):
170
- module .fail_json (msg = 'Note: you must use pymongo 3.0+' )
171
169
srv_info = client .server_info ()
172
- if LooseVersion (srv_info ['version' ]) >= LooseVersion ('3.2' ) and LooseVersion (PyMongoVersion ) <= LooseVersion ('3.2' ):
173
- module .fail_json (msg = ' (Note: you must use pymongo 3.2+ with MongoDB >= 3.2)' )
170
+ if LooseVersion (PyMongoVersion ) <= LooseVersion ('3.2' ):
171
+ module .fail_json (msg = 'Note: you must use pymongo 3.2+' )
172
+ if LooseVersion (srv_info ['version' ]) >= LooseVersion ('3.4' ) and LooseVersion (PyMongoVersion ) <= LooseVersion ('3.4' ):
173
+ module .fail_json (msg = 'Note: you must use pymongo 3.4+ with MongoDB 3.4.x' )
174
+ if LooseVersion (srv_info ['version' ]) >= LooseVersion ('3.6' ) and LooseVersion (PyMongoVersion ) <= LooseVersion ('3.6' ):
175
+ module .fail_json (msg = 'Note: you must use pymongo 3.6+ with MongoDB 3.6.x' )
176
+
174
177
175
178
def check_members (state , module , client , host_name , host_port , host_type ):
176
179
admin_db = client ['admin' ]
@@ -236,7 +239,7 @@ def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwa
236
239
cfg ['members' ].append (new_host )
237
240
admin_db .command ('replSetReconfig' , cfg )
238
241
return
239
- except (OperationFailure , AutoReconnect ), e :
242
+ except (OperationFailure , AutoReconnect ) as e :
240
243
timeout = timeout - 5
241
244
if timeout <= 0 :
242
245
module .fail_json (msg = 'reached timeout while waiting for rs.reconfig(): %s' % str (e ))
@@ -265,7 +268,7 @@ def remove_host(module, client, host_name, timeout=180):
265
268
else :
266
269
fail_msg = "couldn't find member with hostname: {0} in replica set members list" .format (host_name )
267
270
module .fail_json (msg = fail_msg )
268
- except (OperationFailure , AutoReconnect ), e :
271
+ except (OperationFailure , AutoReconnect ) as e :
269
272
timeout = timeout - 5
270
273
if timeout <= 0 :
271
274
module .fail_json (msg = 'reached timeout while waiting for rs.reconfig(): %s' % str (e ))
@@ -318,7 +321,7 @@ def main():
318
321
module = AnsibleModule (
319
322
argument_spec = dict (
320
323
login_user = dict (default = None ),
321
- login_password = dict (default = None ),
324
+ login_password = dict (default = None , no_log = True ),
322
325
login_host = dict (default = 'localhost' ),
323
326
login_port = dict (default = '27017' ),
324
327
replica_set = dict (default = None ),
@@ -374,9 +377,9 @@ def main():
374
377
wait_for_ok_and_master (module , client )
375
378
replica_set_created = True
376
379
module .exit_json (changed = True , host_name = host_name , host_port = host_port , host_type = host_type )
377
- except OperationFailure , e :
380
+ except OperationFailure as e :
378
381
module .fail_json (msg = 'Unable to initiate replica set: %s' % str (e ))
379
- except ConnectionFailure , e :
382
+ except ConnectionFailure as e :
380
383
module .fail_json (msg = 'unable to connect to database: %s' % str (e ))
381
384
382
385
check_compatibility (module , client )
@@ -394,13 +397,13 @@ def main():
394
397
priority = float (module .params ['priority' ]),
395
398
slave_delay = module .params ['slave_delay' ],
396
399
votes = module .params ['votes' ])
397
- except OperationFailure , e :
400
+ except OperationFailure as e :
398
401
module .fail_json (msg = 'Unable to add new member to replica set: %s' % str (e ))
399
402
400
403
elif state == 'absent' :
401
404
try :
402
405
remove_host (module , client , host_name )
403
- except OperationFailure , e :
406
+ except OperationFailure as e :
404
407
module .fail_json (msg = 'Unable to remove member of replica set: %s' % str (e ))
405
408
406
409
module .exit_json (changed = True , host_name = host_name , host_port = host_port , host_type = host_type )
0 commit comments