| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2020, Rhys Campbell <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: mongodb_maintenance |
| 13 |
short_description: Enables or disables maintenance mode for a secondary member. |
| 14 |
description: |
| 15 |
- Enables or disables maintenance mode for a secondary member. |
| 16 |
- Wrapper around the replSetMaintenance command. |
| 17 |
- Performs no actions against a PRIMARY member. |
| 18 |
- When enabled SECONDARY members will not service reads. |
| 19 |
author: Rhys Campbell (@rhysmeister) |
| 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 |
maintenance: |
| 28 |
description: Enable or disable maintenance mode. |
| 29 |
type: bool |
| 30 |
default: false |
| 31 |
notes: |
| 32 |
- Requires the pymongo Python package on the remote host, version 4+.. This |
| 33 |
can be installed using pip or the OS package manager. |
| 34 |
@see U(http://api.mongodb.org/python/current/installation.html) |
| 35 |
requirements: |
| 36 |
- pymongo |
| 37 |
''' |
| 38 |
|
| 39 |
EXAMPLES = r''' |
| 40 |
- name: Enable maintenance mode |
| 41 |
community.mongodb.mongodb_maintenance: |
| 42 |
maintenance: true |
| 43 |
|
| 44 |
- name: Disable maintenance mode |
| 45 |
community.mongodb.mongodb_maintenance: |
| 46 |
maintenance: false |
| 47 |
''' |
| 48 |
|
| 49 |
RETURN = r''' |
| 50 |
changed: |
| 51 |
description: Whether the member was placed into maintenance mode or not. |
| 52 |
returned: success |
| 53 |
type: bool |
| 54 |
msg: |
| 55 |
description: A short description of what happened. |
| 56 |
returned: success |
| 57 |
type: str |
| 58 |
failed: |
| 59 |
description: If something went wrong |
| 60 |
returned: failed |
| 61 |
type: bool |
| 62 |
''' |
| 63 |
|
| 64 |
from ansible.module_utils.basic import AnsibleModule |
| 65 |
from ansible.module_utils._text import to_native |
| 66 |
from ansible_collections.community.mongodb.plugins.module_utils.mongodb_common import ( |
| 67 |
missing_required_lib, |
| 68 |
mongodb_common_argument_spec, |
| 69 |
member_state, |
| 70 |
mongo_auth, |
| 71 |
PYMONGO_IMP_ERR, |
| 72 |
pymongo_found, |
| 73 |
get_mongodb_client, |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
def put_in_maint_mode(client): |
| 78 |
client['admin'].command('replSetMaintenance', True) |
| 79 |
|
| 80 |
|
| 81 |
def remove_maint_mode(client): |
| 82 |
client['admin'].command('replSetMaintenance', False) |
| 83 |
|
| 84 |
|
| 85 |
def main(): |
| 86 |
argument_spec = mongodb_common_argument_spec() |
| 87 |
argument_spec.update( |
| 88 |
maintenance=dict(type='bool', default=False) |
| 89 |
) |
| 90 |
module = AnsibleModule( |
| 91 |
argument_spec=argument_spec, |
| 92 |
supports_check_mode=True, |
| 93 |
required_together=[['login_user', 'login_password']], |
| 94 |
) |
| 95 |
|
| 96 |
if not pymongo_found: |
| 97 |
module.fail_json(msg=missing_required_lib('pymongo'), |
| 98 |
exception=PYMONGO_IMP_ERR) |
| 99 |
|
| 100 |
maintenance = module.params['maintenance'] |
| 101 |
|
| 102 |
result = dict( |
| 103 |
changed=False, |
| 104 |
) |
| 105 |
|
| 106 |
try: |
| 107 |
client = get_mongodb_client(module, directConnection=True) |
| 108 |
client = mongo_auth(module, client, directConnection=True) |
| 109 |
except Exception as excep: |
| 110 |
module.fail_json(msg='Unable to connect to MongoDB: %s' % to_native(excep)) |
| 111 |
|
| 112 |
try: |
| 113 |
state = member_state(client) |
| 114 |
if state == "PRIMARY": |
| 115 |
result["msg"] = "no action taken as member state was PRIMARY" |
| 116 |
elif state == "SECONDARY": |
| 117 |
if maintenance: |
| 118 |
if module.check_mode: |
| 119 |
result["changed"] = True |
| 120 |
result["msg"] = "member was placed into maintenance mode" |
| 121 |
else: |
| 122 |
put_in_maint_mode(client) |
| 123 |
result["changed"] = True |
| 124 |
result["msg"] = "member was placed into maintenance mode" |
| 125 |
else: |
| 126 |
result["msg"] = "No action taken as maintenance parameter is false and member state is SECONDARY" |
| 127 |
elif state == "RECOVERING": |
| 128 |
if maintenance: |
| 129 |
result["msg"] = "no action taken as member is already in a RECOVERING state" |
| 130 |
else: |
| 131 |
if module.check_mode: |
| 132 |
result["changed"] = True |
| 133 |
result["msg"] = "the member was removed from maintenance mode" |
| 134 |
else: |
| 135 |
remove_maint_mode(client) |
| 136 |
result["changed"] = True |
| 137 |
result["msg"] = "the member was removed from maintenance mode" |
| 138 |
else: |
| 139 |
result["msg"] = "no action taken as member state was {0}".format(state) |
| 140 |
except Exception as excep: |
| 141 |
module.fail_json(msg='module encountered an error: %s' % to_native(excep)) |
| 142 |
|
| 143 |
module.exit_json(**result) |
| 144 |
|
| 145 |
|
| 146 |
if __name__ == '__main__': |
| 147 |
main() |