Commit 698d5a1f by Max Rothman

Make mongo_status support special characters in host/un/pw

parent c52660cd
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
DOCUMENTATION = """ DOCUMENTATION = """
--- ---
module: mongo_status module: mongodb_rs_status
short_description: Get the status of a mongo cluster. short_description: Get the status of a replica set of a mongo cluster.
description: description:
- Get the status of a mongo cluster. Provide the same info as rs.status() - Get the status of the replica set of a mongo cluster. Provide the same info as rs.status() or replSetGetStatus.
version_added: "1.9" version_added: "1.9"
author: Feanil Patel author: Feanil Patel
options: options:
...@@ -27,6 +27,10 @@ options: ...@@ -27,6 +27,10 @@ options:
description: description:
- The password to use when authenticating. - The password to use when authenticating.
required: false required: false
auth_database:
description:
- The database to authenticate against.
required: false
""" """
EXAMPLES = ''' EXAMPLES = '''
...@@ -49,6 +53,7 @@ else: ...@@ -49,6 +53,7 @@ else:
pymongo_found = True pymongo_found = True
import json import json
from urllib import quote_plus
def main(): def main():
...@@ -57,6 +62,7 @@ def main(): ...@@ -57,6 +62,7 @@ def main():
port=dict(required=False, type='int', default=27017), port=dict(required=False, type='int', default=27017),
username=dict(required=False, type='str'), username=dict(required=False, type='str'),
password=dict(required=False, type='str'), password=dict(required=False, type='str'),
auth_database=dict(required=False, type='str')
) )
module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=False) module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=False)
...@@ -69,14 +75,18 @@ def main(): ...@@ -69,14 +75,18 @@ def main():
port = module.params.get('port') port = module.params.get('port')
username = module.params.get('username') username = module.params.get('username')
password = module.params.get('password') password = module.params.get('password')
auth_database = module.params.get('auth_database')
if (username and not password) or (password and not username): if (username and not password) or (password and not username):
module.fail_json(msg="Must provide both username and password or neither.") module.fail_json(msg="Must provide both username and password or neither.")
if username: if username:
mongo_uri += "{}:{}@".format(username,password) mongo_uri += "{}:{}@".format(*map(quote_plus, [username,password]))
mongo_uri += "{}:{}".format(host,port) mongo_uri += "{}:{}".format(quote_plus(host),port)
if auth_database:
mongo_uri += '/{}'.format(quote_plus(auth_database))
client = MongoClient(mongo_uri) client = MongoClient(mongo_uri)
status = client.admin.command("replSetGetStatus") status = client.admin.command("replSetGetStatus")
...@@ -85,7 +95,7 @@ def main(): ...@@ -85,7 +95,7 @@ def main():
# jsonify function can process and output without throwing errors on bson # jsonify function can process and output without throwing errors on bson
# types that don't exist in JSON # types that don't exist in JSON
clean = json.loads(json_util.dumps(status)) clean = json.loads(json_util.dumps(status))
module.exit_json(changed=False, status=clean) module.exit_json(changed=False, status=clean)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment