| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2016, Loic Blot <[email protected]> |
| 5 |
# Sponsored by Infopro Digital. http://www.infopro-digital.com/ |
| 6 |
# Sponsored by E.T.A.I. http://www.etai.fr/ |
| 7 |
# |
| 8 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: mongodb_parameter |
| 17 |
short_description: Change an administrative parameter on a MongoDB server |
| 18 |
description: |
| 19 |
- Change an administrative parameter on a MongoDB server. |
| 20 |
version_added: "1.0.0" |
| 21 |
|
| 22 |
extends_documentation_fragment: |
| 23 |
- community.mongodb.login_options |
| 24 |
- community.mongodb.ssl_options |
| 25 |
|
| 26 |
options: |
| 27 |
replica_set: |
| 28 |
description: |
| 29 |
- Replica set to connect to (automatically connects to primary for writes). |
| 30 |
type: str |
| 31 |
param: |
| 32 |
description: |
| 33 |
- MongoDB administrative parameter to modify. |
| 34 |
type: str |
| 35 |
required: true |
| 36 |
value: |
| 37 |
description: |
| 38 |
- MongoDB administrative parameter value to set. |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
param_type: |
| 42 |
description: |
| 43 |
- Define the type of parameter value. |
| 44 |
default: str |
| 45 |
type: str |
| 46 |
choices: [int, str] |
| 47 |
|
| 48 |
notes: |
| 49 |
- Requires the pymongo Python package on the remote host, version 4+. |
| 50 |
- This can be installed using pip or the OS package manager. |
| 51 |
- See also U(https://www.mongodb.com/docs/languages/python/pymongo-driver/current/get-started/download-and-install/) |
| 52 |
requirements: [ "pymongo" ] |
| 53 |
author: "Loic Blot (@nerzhul)" |
| 54 |
''' |
| 55 |
|
| 56 |
EXAMPLES = r''' |
| 57 |
- name: Set MongoDB syncdelay to 60 (this is an int) |
| 58 |
community.mongodb.mongodb_parameter: |
| 59 |
param: syncdelay |
| 60 |
value: 60 |
| 61 |
param_type: int |
| 62 |
''' |
| 63 |
|
| 64 |
RETURN = r''' |
| 65 |
before: |
| 66 |
description: value before modification |
| 67 |
returned: success |
| 68 |
type: str |
| 69 |
after: |
| 70 |
description: value after modification |
| 71 |
returned: success |
| 72 |
type: str |
| 73 |
''' |
| 74 |
|
| 75 |
import traceback |
| 76 |
|
| 77 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 78 |
from ansible.module_utils._text import to_native |
| 79 |
from ansible_collections.community.mongodb.plugins.module_utils.mongodb_common import ( |
| 80 |
missing_required_lib, |
| 81 |
mongodb_common_argument_spec, |
| 82 |
mongo_auth, |
| 83 |
PYMONGO_IMP_ERR, |
| 84 |
pymongo_found, |
| 85 |
OperationFailure, |
| 86 |
get_mongodb_client, |
| 87 |
) |
| 88 |
|
| 89 |
# ========================================= |
| 90 |
# Module execution. |
| 91 |
# |
| 92 |
|
| 93 |
|
| 94 |
def main(): |
| 95 |
argument_spec = mongodb_common_argument_spec() |
| 96 |
argument_spec.update( |
| 97 |
replica_set=dict(default=None), |
| 98 |
param=dict(required=True), |
| 99 |
value=dict(required=True), |
| 100 |
param_type=dict(default="str", choices=['str', 'int']) |
| 101 |
) |
| 102 |
module = AnsibleModule( |
| 103 |
argument_spec=argument_spec, |
| 104 |
supports_check_mode=False, |
| 105 |
required_together=[['login_user', 'login_password']], |
| 106 |
) |
| 107 |
|
| 108 |
if not pymongo_found: |
| 109 |
module.fail_json(msg=missing_required_lib('pymongo'), |
| 110 |
exception=PYMONGO_IMP_ERR) |
| 111 |
|
| 112 |
param = module.params['param'] |
| 113 |
param_type = module.params['param_type'] |
| 114 |
value = module.params['value'] |
| 115 |
|
| 116 |
# Verify parameter is coherent with specified type |
| 117 |
try: |
| 118 |
if param_type == 'int': |
| 119 |
value = int(value) |
| 120 |
except ValueError: |
| 121 |
module.fail_json(msg="value '%s' is not %s" % (value, param_type)) |
| 122 |
|
| 123 |
try: |
| 124 |
client = get_mongodb_client(module, directConnection=True) |
| 125 |
client = mongo_auth(module, client, directConnection=True) |
| 126 |
except Exception as excep: |
| 127 |
module.fail_json(msg='Unable to connect to MongoDB: %s' % to_native(excep)) |
| 128 |
|
| 129 |
db = client.admin |
| 130 |
|
| 131 |
try: |
| 132 |
after_value = db.command("setParameter", **{param: value}) |
| 133 |
except OperationFailure as e: |
| 134 |
module.fail_json(msg="unable to change parameter: %s" % to_native(e), exception=traceback.format_exc()) |
| 135 |
|
| 136 |
if "was" not in after_value: |
| 137 |
module.exit_json(changed=True, msg="Unable to determine old value, assume it changed.") |
| 138 |
else: |
| 139 |
module.exit_json(changed=(value != after_value["was"]), before=after_value["was"], |
| 140 |
after=value) |
| 141 |
|
| 142 |
|
| 143 |
if __name__ == '__main__': |
| 144 |
main() |