| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
DOCUMENTATION = ''' |
| 8 |
--- |
| 9 |
module: vmware_drs_override |
| 10 |
version_added: '5.2.0' |
| 11 |
short_description: Configure DRS behavior for a specific VM in vSphere |
| 12 |
description: |
| 13 |
- This module allows setting a DRS behavior override for individual VMs within a DRS-enabled VMware vSphere cluster. |
| 14 |
options: |
| 15 |
vm_name: |
| 16 |
description: |
| 17 |
- Name of the VM for which the DRS override is set. |
| 18 |
required: true |
| 19 |
type: str |
| 20 |
drs_behavior: |
| 21 |
description: |
| 22 |
- Desired DRS behavior for the VM. |
| 23 |
choices: ['manual', 'partiallyAutomated', 'fullyAutomated'] |
| 24 |
default: 'manual' |
| 25 |
type: str |
| 26 |
extends_documentation_fragment: |
| 27 |
- vmware.vmware.base_options |
| 28 |
author: |
| 29 |
- Sergey Goncharov (@svg1007) |
| 30 |
''' |
| 31 |
|
| 32 |
EXAMPLES = ''' |
| 33 |
- name: Set DRS behavior for a VM |
| 34 |
vmware_drs_override: |
| 35 |
hostname: "vcenter.example.com" |
| 36 |
username: "[email protected]" |
| 37 |
password: "yourpassword" |
| 38 |
port: 443 |
| 39 |
validate_certs: False |
| 40 |
vm_name: "my_vm_name" |
| 41 |
drs_behavior: "manual" |
| 42 |
''' |
| 43 |
|
| 44 |
RETURN = ''' |
| 45 |
changed: |
| 46 |
description: Whether the DRS behavior was changed. |
| 47 |
type: bool |
| 48 |
returned: always |
| 49 |
msg: |
| 50 |
description: A message describing the outcome of the task. |
| 51 |
type: str |
| 52 |
returned: always |
| 53 |
''' |
| 54 |
|
| 55 |
try: |
| 56 |
from pyVmomi import vim, vmodl |
| 57 |
except ImportError: |
| 58 |
pass |
| 59 |
|
| 60 |
from ansible.module_utils.basic import AnsibleModule |
| 61 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import wait_for_task, PyVmomi |
| 62 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 63 |
|
| 64 |
|
| 65 |
class VmwareDrsOverride(PyVmomi): |
| 66 |
def __init__(self, module): |
| 67 |
super(VmwareDrsOverride, self).__init__(module) |
| 68 |
self.vm_name = self.params.get('vm_name', None) |
| 69 |
self.drs_behavior = module.params['drs_behavior'] |
| 70 |
self.params['name'] = self.vm_name |
| 71 |
self.vm = self.get_vm() |
| 72 |
if not self.vm: |
| 73 |
self.module.fail_json(msg="VM '%s' not found." % self.vm_name) |
| 74 |
|
| 75 |
if not self.is_vcenter(): |
| 76 |
self.module.fail_json(msg="DRS configuration is only supported in vCenter environments.") |
| 77 |
|
| 78 |
def set_drs_override(self): |
| 79 |
cluster = self.vm.runtime.host.parent |
| 80 |
|
| 81 |
# Check current DRS settings |
| 82 |
existing_config = next((config for config in cluster.configuration.drsVmConfig if config.key == self.vm), None) |
| 83 |
if existing_config and existing_config.behavior == self.drs_behavior: |
| 84 |
self.module.exit_json(changed=False, msg="DRS behavior is already set to the desired state.") |
| 85 |
|
| 86 |
# Create DRS VM config spec |
| 87 |
drs_vm_config_spec = vim.cluster.DrsVmConfigSpec( |
| 88 |
operation='add', |
| 89 |
info=vim.cluster.DrsVmConfigInfo( |
| 90 |
key=self.vm, |
| 91 |
enabled=True, |
| 92 |
behavior=self.drs_behavior |
| 93 |
) |
| 94 |
) |
| 95 |
|
| 96 |
# Apply the cluster reconfiguration |
| 97 |
cluster_config_spec = vim.cluster.ConfigSpec() |
| 98 |
cluster_config_spec.drsVmConfigSpec = [drs_vm_config_spec] |
| 99 |
try: |
| 100 |
task = cluster.ReconfigureCluster_Task(spec=cluster_config_spec, modify=True) |
| 101 |
wait_for_task(task) |
| 102 |
self.module.exit_json(changed=True, msg="DRS override applied successfully.") |
| 103 |
except vmodl.MethodFault as error: |
| 104 |
self.module.fail_json(msg="Failed to set DRS override: %s" % error.msg) |
| 105 |
|
| 106 |
|
| 107 |
def main(): |
| 108 |
argument_spec = base_argument_spec() |
| 109 |
argument_spec.update(dict( |
| 110 |
vm_name=dict(type='str', required=True), |
| 111 |
drs_behavior=dict(type='str', choices=['manual', 'partiallyAutomated', 'fullyAutomated'], default='manual') |
| 112 |
)) |
| 113 |
|
| 114 |
module = AnsibleModule( |
| 115 |
argument_spec=argument_spec, |
| 116 |
supports_check_mode=True |
| 117 |
) |
| 118 |
|
| 119 |
drs_override = VmwareDrsOverride(module) |
| 120 |
drs_override.set_drs_override() |
| 121 |
|
| 122 |
|
| 123 |
if __name__ == '__main__': |
| 124 |
main() |