| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2022, NetApp, Inc |
| 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 = ''' |
| 11 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 12 |
description: |
| 13 |
- "Enable or disable HA on a cluster" |
| 14 |
extends_documentation_fragment: |
| 15 |
- netapp.ontap.netapp.na_ontap |
| 16 |
module: na_ontap_cluster_ha |
| 17 |
options: |
| 18 |
state: |
| 19 |
choices: ['present', 'absent'] |
| 20 |
type: str |
| 21 |
description: |
| 22 |
- "Whether HA on cluster should be enabled or disabled." |
| 23 |
default: present |
| 24 |
short_description: NetApp ONTAP Manage HA status for cluster |
| 25 |
version_added: 2.6.0 |
| 26 |
''' |
| 27 |
|
| 28 |
EXAMPLES = """ |
| 29 |
- name: Enable HA status for cluster |
| 30 |
netapp.ontap.na_ontap_cluster_ha: |
| 31 |
state: present |
| 32 |
hostname: "{{ netapp_hostname }}" |
| 33 |
username: "{{ netapp_username }}" |
| 34 |
password: "{{ netapp_password }}" |
| 35 |
""" |
| 36 |
|
| 37 |
RETURN = """ |
| 38 |
""" |
| 39 |
|
| 40 |
import traceback |
| 41 |
from ansible.module_utils.basic import AnsibleModule |
| 42 |
from ansible.module_utils._text import to_native |
| 43 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 44 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 45 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 46 |
|
| 47 |
|
| 48 |
class NetAppOntapClusterHA: |
| 49 |
""" |
| 50 |
object initialize and class methods |
| 51 |
""" |
| 52 |
def __init__(self): |
| 53 |
|
| 54 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 55 |
self.argument_spec.update(dict( |
| 56 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 57 |
)) |
| 58 |
self.module = AnsibleModule( |
| 59 |
argument_spec=self.argument_spec, |
| 60 |
supports_check_mode=True |
| 61 |
) |
| 62 |
|
| 63 |
self.na_helper = NetAppModule() |
| 64 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 65 |
# Set up Rest API |
| 66 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 67 |
self.use_rest = self.rest_api.is_rest() |
| 68 |
|
| 69 |
if not self.use_rest: |
| 70 |
if not netapp_utils.has_netapp_lib(): |
| 71 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 72 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 73 |
|
| 74 |
def modify_cluster_ha(self, configure): |
| 75 |
""" |
| 76 |
Enable or disable HA on cluster |
| 77 |
:return: None |
| 78 |
""" |
| 79 |
if self.use_rest: |
| 80 |
return self.modify_cluster_ha_rest(configure) |
| 81 |
|
| 82 |
cluster_ha_modify = netapp_utils.zapi.NaElement.create_node_with_children( |
| 83 |
'cluster-ha-modify', **{'ha-configured': configure}) |
| 84 |
try: |
| 85 |
self.server.invoke_successfully(cluster_ha_modify, |
| 86 |
enable_tunneling=True) |
| 87 |
except netapp_utils.zapi.NaApiError as error: |
| 88 |
self.module.fail_json(msg='Error modifying cluster HA to %s: %s' |
| 89 |
% (configure, to_native(error)), |
| 90 |
exception=traceback.format_exc()) |
| 91 |
|
| 92 |
def get_cluster_ha_enabled(self): |
| 93 |
""" |
| 94 |
Get current cluster HA details |
| 95 |
:return: dict if enabled, None if disabled |
| 96 |
""" |
| 97 |
if self.use_rest: |
| 98 |
return self.get_cluster_ha_enabled_rest() |
| 99 |
cluster_ha_get = netapp_utils.zapi.NaElement('cluster-ha-get') |
| 100 |
try: |
| 101 |
result = self.server.invoke_successfully(cluster_ha_get, |
| 102 |
enable_tunneling=True) |
| 103 |
except netapp_utils.zapi.NaApiError: |
| 104 |
self.module.fail_json(msg='Error fetching cluster HA details', |
| 105 |
exception=traceback.format_exc()) |
| 106 |
cluster_ha_info = result.get_child_by_name('attributes').get_child_by_name('cluster-ha-info') |
| 107 |
if cluster_ha_info.get_child_content('ha-configured') == 'true': |
| 108 |
return {'ha-configured': True} |
| 109 |
return None |
| 110 |
|
| 111 |
def get_cluster_ha_enabled_rest(self): |
| 112 |
api = 'private/cli/cluster/ha' |
| 113 |
params = {'fields': 'configured'} |
| 114 |
record, error = rest_generic.get_one_record(self.rest_api, api, params) |
| 115 |
if error: |
| 116 |
self.module.fail_json(msg='Error fetching cluster HA details: %s' % to_native(error)) |
| 117 |
return {'ha-configured': True} if record['configured'] else None |
| 118 |
|
| 119 |
def modify_cluster_ha_rest(self, configure): |
| 120 |
api = 'private/cli/cluster/ha' |
| 121 |
body = {'configured': True if configure == "true" else False} |
| 122 |
dummy, error = rest_generic.patch_async(self.rest_api, api, None, body) |
| 123 |
if error: |
| 124 |
self.module.fail_json(msg='Error modifying cluster HA to %s: %s' % (configure, to_native(error))) |
| 125 |
|
| 126 |
def apply(self): |
| 127 |
""" |
| 128 |
Apply action to cluster HA |
| 129 |
""" |
| 130 |
current = self.get_cluster_ha_enabled() |
| 131 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 132 |
if not self.module.check_mode: |
| 133 |
if cd_action == 'create': |
| 134 |
self.modify_cluster_ha("true") |
| 135 |
elif cd_action == 'delete': |
| 136 |
self.modify_cluster_ha("false") |
| 137 |
|
| 138 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action) |
| 139 |
self.module.exit_json(**result) |
| 140 |
|
| 141 |
|
| 142 |
def main(): |
| 143 |
""" |
| 144 |
Create object and call apply |
| 145 |
""" |
| 146 |
ha_obj = NetAppOntapClusterHA() |
| 147 |
ha_obj.apply() |
| 148 |
|
| 149 |
|
| 150 |
if __name__ == '__main__': |
| 151 |
main() |