From 2ef3a97b8fcd619a6a2e9faf343d5caf72deb228 Mon Sep 17 00:00:00 2001 From: "nina.loser" Date: Thu, 11 May 2023 19:56:45 +0200 Subject: [PATCH 1/6] Add changelog file and add the function that it trys to find an placement recommendation 10 times every 1 minute --- changelogs/fragments/-vmware_vmotion.yaml | 4 ++ plugins/modules/vmware_vmotion.py | 46 ++++++++++++++--------- 2 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 changelogs/fragments/-vmware_vmotion.yaml diff --git a/changelogs/fragments/-vmware_vmotion.yaml b/changelogs/fragments/-vmware_vmotion.yaml new file mode 100644 index 000000000..172aee311 --- /dev/null +++ b/changelogs/fragments/-vmware_vmotion.yaml @@ -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/) \ No newline at end of file diff --git a/plugins/modules/vmware_vmotion.py b/plugins/modules/vmware_vmotion.py index e9d39c920..04e15a097 100644 --- a/plugins/modules/vmware_vmotion.py +++ b/plugins/modules/vmware_vmotion.py @@ -10,10 +10,11 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type +import time DOCUMENTATION = r''' --- -module: vmware_vmotion +module: vmware_vmotion.yaml short_description: Move a virtual machine using vMotion, and/or its vmdks using storage vMotion. description: - Using VMware vCenter, move a virtual machine using vMotion to a different @@ -93,7 +94,7 @@ EXAMPLES = r''' - name: Perform vMotion of virtual machine - community.vmware.vmware_vmotion: + community.vmware.vmware_vmotion.yaml: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -102,7 +103,7 @@ delegate_to: localhost - name: Perform vMotion of virtual machine - community.vmware.vmware_vmotion: + community.vmware.vmware_vmotion.yaml: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -111,7 +112,7 @@ delegate_to: localhost - name: Perform vMotion of virtual machine to resource_pool - community.vmware.vmware_vmotion: + community.vmware.vmware_vmotion.yaml: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -121,7 +122,7 @@ delegate_to: localhost - name: Perform storage vMotion of virtual machine - community.vmware.vmware_vmotion: + community.vmware.vmware_vmotion.yaml: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -130,7 +131,7 @@ delegate_to: localhost - name: Perform storage vMotion and host vMotion of virtual machine - community.vmware.vmware_vmotion: + community.vmware.vmware_vmotion.yaml: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -140,7 +141,7 @@ delegate_to: localhost - name: Perform storage vMotion to a Storage Cluster and vMotion to a Cluster of virtual machine - community.vmware.vmware_vmotion: + community.vmware.vmware_vmotion.yaml: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -475,16 +476,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: " % 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): """ From b1022e37b96a255aeba2b2189df6c49a4aeda895 Mon Sep 17 00:00:00 2001 From: "nina.loser" Date: Fri, 12 May 2023 10:25:11 +0200 Subject: [PATCH 2/6] miss replace yaml --- plugins/modules/vmware_vmotion.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/modules/vmware_vmotion.py b/plugins/modules/vmware_vmotion.py index 04e15a097..94ea19b28 100644 --- a/plugins/modules/vmware_vmotion.py +++ b/plugins/modules/vmware_vmotion.py @@ -14,7 +14,7 @@ DOCUMENTATION = r''' --- -module: vmware_vmotion.yaml +module: vmware_vmotion short_description: Move a virtual machine using vMotion, and/or its vmdks using storage vMotion. description: - Using VMware vCenter, move a virtual machine using vMotion to a different @@ -94,7 +94,7 @@ EXAMPLES = r''' - name: Perform vMotion of virtual machine - community.vmware.vmware_vmotion.yaml: + community.vmware.vmware_vmotion: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -103,7 +103,7 @@ delegate_to: localhost - name: Perform vMotion of virtual machine - community.vmware.vmware_vmotion.yaml: + community.vmware.vmware_vmotion: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -112,7 +112,7 @@ delegate_to: localhost - name: Perform vMotion of virtual machine to resource_pool - community.vmware.vmware_vmotion.yaml: + community.vmware.vmware_vmotion: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -122,7 +122,7 @@ delegate_to: localhost - name: Perform storage vMotion of virtual machine - community.vmware.vmware_vmotion.yaml: + community.vmware.vmware_vmotion: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -131,7 +131,7 @@ delegate_to: localhost - name: Perform storage vMotion and host vMotion of virtual machine - community.vmware.vmware_vmotion.yaml: + community.vmware.vmware_vmotion: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' @@ -141,7 +141,7 @@ delegate_to: localhost - name: Perform storage vMotion to a Storage Cluster and vMotion to a Cluster of virtual machine - community.vmware.vmware_vmotion.yaml: + community.vmware.vmware_vmotion: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' From d8f5b9ee5a758636784f34c60fe2ee68f99eb0a9 Mon Sep 17 00:00:00 2001 From: "nina.loser" Date: Fri, 12 May 2023 10:28:41 +0200 Subject: [PATCH 3/6] Add the Pull Request Nr --- .../{-vmware_vmotion.yaml => 1735-vmware_vmotion.yaml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename changelogs/fragments/{-vmware_vmotion.yaml => 1735-vmware_vmotion.yaml} (96%) diff --git a/changelogs/fragments/-vmware_vmotion.yaml b/changelogs/fragments/1735-vmware_vmotion.yaml similarity index 96% rename from changelogs/fragments/-vmware_vmotion.yaml rename to changelogs/fragments/1735-vmware_vmotion.yaml index 172aee311..b59e917c9 100644 --- a/changelogs/fragments/-vmware_vmotion.yaml +++ b/changelogs/fragments/1735-vmware_vmotion.yaml @@ -1,4 +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/) \ No newline at end of file + (https://github.com/ansible-collections/community.vmware/pull/1735) \ No newline at end of file From c560446554b5ba8caa9a0513e0404042ddb19d60 Mon Sep 17 00:00:00 2001 From: "nina.loser" Date: Fri, 12 May 2023 12:21:51 +0200 Subject: [PATCH 4/6] Right import --- plugins/modules/vmware_vmotion.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/vmware_vmotion.py b/plugins/modules/vmware_vmotion.py index 94ea19b28..e1f492dfc 100644 --- a/plugins/modules/vmware_vmotion.py +++ b/plugins/modules/vmware_vmotion.py @@ -10,8 +10,6 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -import time - DOCUMENTATION = r''' --- module: vmware_vmotion @@ -173,6 +171,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 ( From 34f585cdf37c515c75bb26ed6b86c0519e633b0a Mon Sep 17 00:00:00 2001 From: "nina.loser" Date: Thu, 22 Jun 2023 11:11:39 +0200 Subject: [PATCH 5/6] Linter --- plugins/modules/vmware_vmotion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/vmware_vmotion.py b/plugins/modules/vmware_vmotion.py index e1f492dfc..54acb537d 100644 --- a/plugins/modules/vmware_vmotion.py +++ b/plugins/modules/vmware_vmotion.py @@ -488,7 +488,7 @@ def set_placement(self): 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: " % vm_id) + self.module.fail_json(msg="Failed to find a placement recommendation for vm: " % str(vm_id)) counter = counter + 1 time.sleep(60) placement = self.cluster_object.PlaceVm(placement_spec) From 0d08c80fb88718d450131582517e811f1490d73e Mon Sep 17 00:00:00 2001 From: "nina.loser" Date: Thu, 22 Jun 2023 14:03:28 +0200 Subject: [PATCH 6/6] Linter --- plugins/modules/vmware_vmotion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/vmware_vmotion.py b/plugins/modules/vmware_vmotion.py index 54acb537d..5d287280d 100644 --- a/plugins/modules/vmware_vmotion.py +++ b/plugins/modules/vmware_vmotion.py @@ -488,7 +488,7 @@ def set_placement(self): 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: " % str(vm_id)) + 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)