| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2023, Hewlett Packard Enterprise Development LP |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: vsan_health_silent_checks |
| 16 |
version_added: '3.6.0' |
| 17 |
short_description: Silence vSAN health checks |
| 18 |
description: |
| 19 |
- Take a list of vSAN health checks and silence them |
| 20 |
- Re-enable alerts for previously silenced health checks |
| 21 |
author: |
| 22 |
- Philipp Fruck (@p-fruck) |
| 23 |
requirements: |
| 24 |
- vSAN Management SDK, which needs to be downloaded from VMware and installed manually. |
| 25 |
options: |
| 26 |
cluster_name: |
| 27 |
description: |
| 28 |
- Name of the vSAN cluster. |
| 29 |
type: str |
| 30 |
required: true |
| 31 |
checks: |
| 32 |
description: |
| 33 |
- The checks to silence. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
required: false |
| 37 |
state: |
| 38 |
description: |
| 39 |
- The state of the health checks. |
| 40 |
- If set to V(present), all given health checks will be silenced. |
| 41 |
- If set to V(absent), all given health checks will be removed from the list of silent checks. |
| 42 |
default: 'present' |
| 43 |
choices: [ 'present', 'absent' ] |
| 44 |
type: str |
| 45 |
extends_documentation_fragment: |
| 46 |
- vmware.vmware.base_options |
| 47 |
''' |
| 48 |
|
| 49 |
EXAMPLES = r''' |
| 50 |
- name: Disable the vSAN Support Insight health check |
| 51 |
community.vmware.vsan_health_silent_checks: |
| 52 |
hostname: "{{ vcenter_hostname }}" |
| 53 |
username: "{{ vcenter_username }}" |
| 54 |
password: "{{ vcenter_password }}" |
| 55 |
checks: vsanenablesupportinsight |
| 56 |
cluster_name: 'vSAN01' |
| 57 |
delegate_to: localhost |
| 58 |
|
| 59 |
- name: Re-enable health check alerts for release catalog and HCL DB |
| 60 |
community.vmware.vsan_health_silent_checks: |
| 61 |
hostname: "{{ vcenter_hostname }}" |
| 62 |
username: "{{ vcenter_username }}" |
| 63 |
password: "{{ vcenter_password }}" |
| 64 |
checks: |
| 65 |
- releasecataloguptodate |
| 66 |
- autohclupdate |
| 67 |
state: absent |
| 68 |
cluster_name: 'vSAN01' |
| 69 |
delegate_to: localhost |
| 70 |
''' |
| 71 |
|
| 72 |
RETURN = r'''# |
| 73 |
''' |
| 74 |
|
| 75 |
|
| 76 |
import traceback |
| 77 |
|
| 78 |
VSANPYTHONSDK_IMP_ERR = None |
| 79 |
try: |
| 80 |
import vsanapiutils |
| 81 |
HAS_VSANPYTHONSDK = True |
| 82 |
except ImportError: |
| 83 |
VSANPYTHONSDK_IMP_ERR = traceback.format_exc() |
| 84 |
HAS_VSANPYTHONSDK = False |
| 85 |
|
| 86 |
from ansible.module_utils.basic import AnsibleModule |
| 87 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 88 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 89 |
|
| 90 |
|
| 91 |
class VsanApi(PyVmomi): |
| 92 |
def __init__(self, module): |
| 93 |
super(VsanApi, self).__init__(module) |
| 94 |
|
| 95 |
client_stub = self.si._GetStub() |
| 96 |
ssl_context = client_stub.schemeArgs.get('context') |
| 97 |
apiVersion = vsanapiutils.GetLatestVmodlVersion(module.params['hostname']) |
| 98 |
vcMos = vsanapiutils.GetVsanVcMos(client_stub, context=ssl_context, version=apiVersion) |
| 99 |
self.vsanClusterHealthSystem = vcMos['vsan-cluster-health-system'] |
| 100 |
self.get_cluster() |
| 101 |
|
| 102 |
def get_cluster(self): |
| 103 |
cluster_name = self.params.get('cluster_name') |
| 104 |
self.cluster = self.find_cluster_by_name(cluster_name=cluster_name) |
| 105 |
if self.cluster is None: |
| 106 |
self.module.fail_json(msg=f"Cluster {cluster_name} does not exist.") |
| 107 |
|
| 108 |
def process_state(self): |
| 109 |
kwargs = {'cluster': self.cluster} |
| 110 |
silent_checks = self.vsanClusterHealthSystem.VsanHealthGetVsanClusterSilentChecks(**kwargs) |
| 111 |
|
| 112 |
state = self.params.get('state') |
| 113 |
if state == 'present': |
| 114 |
checks = [check for check in self.params.get('checks') if check not in silent_checks] |
| 115 |
kwargs['addSilentChecks'] = checks |
| 116 |
else: |
| 117 |
checks = [check for check in self.params.get('checks') if check in silent_checks] |
| 118 |
kwargs['removeSilentChecks'] = checks |
| 119 |
|
| 120 |
if not checks: |
| 121 |
return False |
| 122 |
|
| 123 |
success = self.vsanClusterHealthSystem.VsanHealthSetVsanClusterSilentChecks(**kwargs) |
| 124 |
if not success: |
| 125 |
raise Exception(f"Unknown error modifying checks {checks}") |
| 126 |
|
| 127 |
return True |
| 128 |
|
| 129 |
|
| 130 |
def main(): |
| 131 |
argument_spec = base_argument_spec() |
| 132 |
|
| 133 |
argument_spec.update(dict( |
| 134 |
checks=dict(type='list', elements='str', required=False), |
| 135 |
cluster_name=dict(type='str', required=True), |
| 136 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 137 |
)) |
| 138 |
|
| 139 |
module = AnsibleModule( |
| 140 |
argument_spec=argument_spec, |
| 141 |
) |
| 142 |
|
| 143 |
api = VsanApi(module) |
| 144 |
try: |
| 145 |
changed = api.process_state() |
| 146 |
module.exit_json(changed=changed) |
| 147 |
except Exception as e: |
| 148 |
module.fail_json(msg=str(e)) |
| 149 |
|
| 150 |
|
| 151 |
if __name__ == '__main__': |
| 152 |
main() |