11#!/usr/bin/python
22
3- # (c) 2015-2016 , Sergei Antipov, 2GIS LLC
3+ # (c) 2015-2018 , Sergei Antipov, 2GIS LLC
44#
55# This file is part of Ansible
66#
2323short_description: Adds or removes a node from a MongoDB Replica Set.
2424description:
2525 - 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 "
2727options:
2828 login_user:
2929 description:
105105 default: present
106106 choices: [ "present", "absent" ]
107107notes:
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
109109 can be installed using pip or the OS package manager. @see http://api.mongodb.org/python/current/installation.html
110110requirements: [ "pymongo" ]
111111author: "Sergei Antipov @UnderGreen"
166166# MongoDB module specific support methods.
167167#
168168def check_compatibility (module , client ):
169- if LooseVersion (PyMongoVersion ) <= LooseVersion ('3.0' ):
170- module .fail_json (msg = 'Note: you must use pymongo 3.0+' )
171169 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+
174177
175178def check_members (state , module , client , host_name , host_port , host_type ):
176179 admin_db = client ['admin' ]
@@ -236,7 +239,7 @@ def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwa
236239 cfg ['members' ].append (new_host )
237240 admin_db .command ('replSetReconfig' , cfg )
238241 return
239- except (OperationFailure , AutoReconnect ), e :
242+ except (OperationFailure , AutoReconnect ) as e :
240243 timeout = timeout - 5
241244 if timeout <= 0 :
242245 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):
265268 else :
266269 fail_msg = "couldn't find member with hostname: {0} in replica set members list" .format (host_name )
267270 module .fail_json (msg = fail_msg )
268- except (OperationFailure , AutoReconnect ), e :
271+ except (OperationFailure , AutoReconnect ) as e :
269272 timeout = timeout - 5
270273 if timeout <= 0 :
271274 module .fail_json (msg = 'reached timeout while waiting for rs.reconfig(): %s' % str (e ))
@@ -318,7 +321,7 @@ def main():
318321 module = AnsibleModule (
319322 argument_spec = dict (
320323 login_user = dict (default = None ),
321- login_password = dict (default = None ),
324+ login_password = dict (default = None , no_log = True ),
322325 login_host = dict (default = 'localhost' ),
323326 login_port = dict (default = '27017' ),
324327 replica_set = dict (default = None ),
@@ -374,9 +377,9 @@ def main():
374377 wait_for_ok_and_master (module , client )
375378 replica_set_created = True
376379 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 :
378381 module .fail_json (msg = 'Unable to initiate replica set: %s' % str (e ))
379- except ConnectionFailure , e :
382+ except ConnectionFailure as e :
380383 module .fail_json (msg = 'unable to connect to database: %s' % str (e ))
381384
382385 check_compatibility (module , client )
@@ -394,13 +397,13 @@ def main():
394397 priority = float (module .params ['priority' ]),
395398 slave_delay = module .params ['slave_delay' ],
396399 votes = module .params ['votes' ])
397- except OperationFailure , e :
400+ except OperationFailure as e :
398401 module .fail_json (msg = 'Unable to add new member to replica set: %s' % str (e ))
399402
400403 elif state == 'absent' :
401404 try :
402405 remove_host (module , client , host_name )
403- except OperationFailure , e :
406+ except OperationFailure as e :
404407 module .fail_json (msg = 'Unable to remove member of replica set: %s' % str (e ))
405408
406409 module .exit_json (changed = True , host_name = host_name , host_port = host_port , host_type = host_type )
0 commit comments