45
45
- The port to connect to
46
46
required: false
47
47
default: 27017
48
+ login_database:
49
+ description:
50
+ - The database where login credentials are stored
51
+ required: false
52
+ default: admin
48
53
replica_set:
49
54
description:
50
55
- Replica set to connect to (automatically connects to primary for writes)
69
74
description:
70
75
- Whether to use an SSL connection when connecting to the database
71
76
default: False
77
+ ssl_cert_reqs:
78
+ description:
79
+ - Specifies whether a certificate is required from the other side of the connection, and whether it will be validated if provided.
80
+ required: false
81
+ default: "CERT_REQUIRED"
82
+ choices: ["CERT_REQUIRED", "CERT_OPTIONAL", "CERT_NONE"]
72
83
build_indexes:
73
84
description:
74
85
- Determines whether the mongod builds indexes on this member.
146
157
sample: "replica"
147
158
'''
148
159
import ConfigParser
160
+ import ssl as ssl_lib
149
161
import time
150
162
from distutils .version import LooseVersion
151
163
try :
@@ -324,11 +336,13 @@ def main():
324
336
login_password = dict (default = None , no_log = True ),
325
337
login_host = dict (default = 'localhost' ),
326
338
login_port = dict (default = '27017' ),
339
+ login_database = dict (default = "admin" ),
327
340
replica_set = dict (default = None ),
328
341
host_name = dict (default = 'localhost' ),
329
342
host_port = dict (default = '27017' ),
330
343
host_type = dict (default = 'replica' , choices = ['replica' ,'arbiter' ]),
331
- ssl = dict (default = 'false' ),
344
+ ssl = dict (default = False , type = 'bool' ),
345
+ ssl_cert_reqs = dict (default = 'CERT_REQUIRED' , choices = ['CERT_NONE' , 'CERT_OPTIONAL' , 'CERT_REQUIRED' ]),
332
346
build_indexes = dict (type = 'bool' , default = 'yes' ),
333
347
hidden = dict (type = 'bool' , default = 'no' ),
334
348
priority = dict (default = '1.0' ),
@@ -345,6 +359,7 @@ def main():
345
359
login_password = module .params ['login_password' ]
346
360
login_host = module .params ['login_host' ]
347
361
login_port = module .params ['login_port' ]
362
+ login_database = module .params ['login_database' ]
348
363
replica_set = module .params ['replica_set' ]
349
364
host_name = module .params ['host_name' ]
350
365
host_port = module .params ['host_port' ]
@@ -359,15 +374,36 @@ def main():
359
374
if replica_set is None :
360
375
module .fail_json (msg = 'replica_set parameter is required' )
361
376
else :
362
- client = MongoClient (login_host , int (login_port ), replicaSet = replica_set ,
363
- ssl = ssl , serverSelectionTimeoutMS = 5000 )
364
-
377
+ connection_params = {
378
+ "host" : login_host ,
379
+ "port" : int (login_port ),
380
+ "username" : login_user ,
381
+ "password" : login_password ,
382
+ "authsource" : login_database ,
383
+ "serverselectiontimeoutms" : 5000 ,
384
+ "replicaset" : replica_set ,
385
+ }
386
+
387
+ if ssl :
388
+ connection_params ["ssl" ] = ssl
389
+ connection_params ["ssl_cert_reqs" ] = getattr (ssl_lib , module .params ['ssl_cert_reqs' ])
390
+
391
+ client = MongoClient (** connection_params )
365
392
authenticate (client , login_user , login_password )
366
393
client ['admin' ].command ('replSetGetStatus' )
367
394
368
395
except ServerSelectionTimeoutError :
369
396
try :
370
- client = MongoClient (login_host , int (login_port ), ssl = ssl )
397
+ connection_params = {
398
+ "host" : login_host ,
399
+ "port" : int (login_port ),
400
+ }
401
+
402
+ if ssl :
403
+ connection_params ["ssl" ] = ssl
404
+ connection_params ["ssl_cert_reqs" ] = getattr (ssl_lib , module .params ['ssl_cert_reqs' ])
405
+
406
+ client = MongoClient (** connection_params )
371
407
authenticate (client , login_user , login_password )
372
408
if state == 'present' :
373
409
new_host = { '_id' : 0 , 'host' : "{0}:{1}" .format (host_name , host_port ) }
0 commit comments