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_vmotion - find an placement recommendation 10 times every 1 minute #1735

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions changelogs/fragments/1735-vmware_vmotion.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
minor_changes:
- vmware_vmotion - Add the function that it trys to find an placement recommendation 10 times every 1 minute
(https://github.com/ansible-collections/community.vmware/pull/1735)
34 changes: 23 additions & 11 deletions plugins/modules/vmware_vmotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = r'''
---
module: vmware_vmotion
Expand Down Expand Up @@ -169,6 +168,8 @@
except ImportError:
pass

import time

from ansible.module_utils._text import to_native
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.vmware.plugins.module_utils.vmware import (
Expand Down Expand Up @@ -472,16 +473,27 @@ def set_placement(self):
storagePods = [self.datastore_cluster_object]
else:
storagePods = None
placement_spec = vim.cluster.PlacementSpec(storagePods=storagePods,
hosts=self.cluster_hosts,
vm=self.vm,
relocateSpec=relocate_spec)
placement = self.cluster_object.PlaceVm(placement_spec)

if self.host_object is None:
self.host_object = placement.recommendations[0].action[0].targetHost
if self.datastore_object is None:
self.datastore_object = placement.recommendations[0].action[0].relocateSpec.datastore

if self.host_object is None or self.datastore_object is None:
placement_spec = vim.cluster.PlacementSpec(storagePods=storagePods,
hosts=self.cluster_hosts,
vm=self.vm,
relocateSpec=relocate_spec)
placement = self.cluster_object.PlaceVm(placement_spec)

counter = 0 # Try it 10 times every minute
while len(placement.recommendations) < 1:
if counter == 10:
vm_id = self.vm_uuid or self.vm_name or self.moid
self.module.fail_json(msg="Failed to find a placement recommendation for vm: %s" % vm_id)
counter = counter + 1
time.sleep(60)
placement = self.cluster_object.PlaceVm(placement_spec)

if self.host_object is None:
self.host_object = placement.recommendations[0].action[0].targetHost
if self.datastore_object is None:
self.datastore_object = placement.recommendations[0].action[0].relocateSpec.datastore

def get_vm(self):
"""
Expand Down