From f476a072745c7ab9e76a5db1b2b48fc0f2dc5f7e Mon Sep 17 00:00:00 2001 From: Nina2244 <26925042+Nina2244@users.noreply.github.com> Date: Sat, 27 May 2023 20:04:50 +0200 Subject: [PATCH] vmware_cluster_drs_recommendations - add a modul to apply the drs recommendations (#1736) vmware_cluster_drs_recommendations - add a modul to apply the drs recommendations SUMMARY Add the Module to apply the drs recommendations. ISSUE TYPE New Module Pull Request COMPONENT NAME vmware_cluster_drs_recommendations ADDITIONAL INFORMATION Reviewed-by: Mario Lenz --- ...36-vmware_cluster_drs_recommendations.yaml | 3 + .../vmware_cluster_drs_recommendations.py | 119 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 changelogs/fragments/1736-vmware_cluster_drs_recommendations.yaml create mode 100644 plugins/modules/vmware_cluster_drs_recommendations.py diff --git a/changelogs/fragments/1736-vmware_cluster_drs_recommendations.yaml b/changelogs/fragments/1736-vmware_cluster_drs_recommendations.yaml new file mode 100644 index 000000000..93cd1154c --- /dev/null +++ b/changelogs/fragments/1736-vmware_cluster_drs_recommendations.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - vmware_cluster_drs_recommendations - Add the Module to apply the drs recommendations (https://github.com/ansible-collections/community.vmware/pull/1736) diff --git a/plugins/modules/vmware_cluster_drs_recommendations.py b/plugins/modules/vmware_cluster_drs_recommendations.py new file mode 100644 index 000000000..2939aa784 --- /dev/null +++ b/plugins/modules/vmware_cluster_drs_recommendations.py @@ -0,0 +1,119 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright: (c) 2023, Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +DOCUMENTATION = r''' +--- +module: vmware_cluster_drs_recommendations +version_added: '3.7.0' +short_description: Apply DRS Recommendations +description: + - Apply DRS Recommendations for Cluster. +author: +- Nina Loser (@Nina2244) +options: + cluster_name: + description: + - The name of the cluster to be managed. + type: str + required: true + datacenter: + description: + - The name of the datacenter. + type: str + required: true + aliases: [ datacenter_name ] +extends_documentation_fragment: +- community.vmware.vmware.documentation + +''' + +EXAMPLES = r''' +- name: Apply DRS Recommendations for Cluster + community.vmware.vmware_cluster: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + datacenter_name: datacenter + cluster_name: cluster + delegate_to: localhost +''' + +RETURN = r''' +result: + description: + - list of the recommendations + - What server moved from which host to which host. + returned: always + type: list + sample: ["server1 move from host1 to host2.", "server2 move from host1 to host2."] +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible_collections.community.vmware.plugins.module_utils.vmware import ( + PyVmomi, + find_datacenter_by_name, + vmware_argument_spec, + wait_for_task) + + +class VMwareCluster(PyVmomi): + def __init__(self, module): + super(VMwareCluster, self).__init__(module) + self.cluster_name = module.params['cluster_name'] + self.datacenter_name = module.params['datacenter'] + self.datacenter = None + self.cluster = None + + self.datacenter = find_datacenter_by_name(self.content, self.datacenter_name) + if self.datacenter is None: + self.module.fail_json(msg="Datacenter %s does not exist." % self.datacenter_name) + + self.cluster = self.find_cluster_by_name(cluster_name=self.cluster_name, datacenter_name=self.datacenter) + if self.cluster is None: + self.module.fail_json(msg="Cluster %s does not exist." % self.cluster_name) + + def recommendations(self): + results = [] + changed = False + self.cluster.RefreshRecommendation() + if len(self.cluster.recommendation) == 0: + self.module.exit_json(changed=changed, result="No recommendations.") + else: + for index, recommendation in enumerate(self.cluster.recommendation): + results.append("%s move from %s to %s." % (recommendation.action[0].target.name, + recommendation.action[0].drsMigration.source.name, + recommendation.action[0].drsMigration.destination.name)) + if not self.module.check_mode: + task = self.cluster.ApplyRecommendation(recommendation.key) + changed = True + if index == len(self.cluster.recommendation) - 1 and hasattr(task, 'info'): + wait_for_task(task) + self.module.exit_json(changed=changed, result=results) + + +def main(): + argument_spec = vmware_argument_spec() + argument_spec.update(dict( + cluster_name=dict(type='str', required=True), + datacenter=dict(type='str', required=True, aliases=['datacenter_name']), + )) + + module = AnsibleModule( + argument_spec=argument_spec, + supports_check_mode=True, + ) + + vmware_cluster = VMwareCluster(module) + vmware_cluster.recommendations() + + +if __name__ == '__main__': + main()