Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vmware_cluster_drs_recommendations - add a modul to apply the drs recommendations #1736

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
119 changes: 119 additions & 0 deletions plugins/modules/vmware_cluster_drs_recommendations.py
Original file line number Diff line number Diff line change
@@ -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
Nina2244 marked this conversation as resolved.
Show resolved Hide resolved
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()