Skip to content

Commit

Permalink
vmware_guest_info - Make "datacenter" property optional (ansible-coll…
Browse files Browse the repository at this point in the history
…ections#2241)

SUMMARY

We run a big VMware environment featuring multiple vCenters and multiple datacenters within them.
When automating the deployment of VMs at the moment we search for existing machines with the name of the to be deployed machine in order to perform clean-up activities. At the moment we use the guest find module, but it's slow.
Therefore we looked into the guest_info module and found that if the VM is unique, the datacenter property is never actually used as it is only relevant in case multiple VMs have been found.
In case multiple VMs are found, the folder and datacenter attributes are used to decide on "the right one".
Therefore, the datacenter property could as well be an optional parameter on the module and it could simply fail in case there is more than one VM matching and "datacenter" as well as "folder" are required.

ISSUE TYPE


Feature Pull Request

COMPONENT NAME

vmware_guest_info.py
vmware.py

ADDITIONAL INFORMATION

Reviewed-by: Mario Lenz <[email protected]>
  • Loading branch information
PfurtschellerP authored Nov 29, 2024
1 parent 3bcd926 commit 5bca41c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- vmware_guest_info - `datacenter` property is now optional as it only required in cases where the VM is not uniquely identified by `name`.
- vmware.py - Add logic for handling the case where the `datacenter` property is not provided.
4 changes: 2 additions & 2 deletions plugins/module_utils/vmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,9 +1226,9 @@ def get_vm(self):
# following code tries to find user desired one depending upon the folder specified.
if len(vms) > 1:
# We have found multiple virtual machines, decide depending upon folder value
if self.params['folder'] is None:
if self.params['folder'] is None or self.params['datacenter'] is None:
self.module.fail_json(msg="Multiple virtual machines with same name [%s] found, "
"Folder value is a required parameter to find uniqueness "
"try to specify folder and / or datacenter to ensure uniqueness "
"of the virtual machine" % self.params['name'],
details="Please see documentation of the vmware_guest module "
"for folder parameter.")
Expand Down
7 changes: 4 additions & 3 deletions plugins/modules/vmware_guest_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
folder:
description:
- Destination folder, absolute or relative path to find an existing guest.
- This is required if O(name) is supplied.
- This is required if O(name) is supplied and not unique.
- The folder should include the datacenter. ESX's datacenter is ha-datacenter
- 'Examples:'
- ' folder: /ha-datacenter/vm'
Expand All @@ -64,7 +64,8 @@
datacenter:
description:
- Destination datacenter for the deploy operation
required: true
- This is required if O(name) is supplied and not unique.
required: false
type: str
tags:
description:
Expand Down Expand Up @@ -262,7 +263,7 @@ def main():
use_instance_uuid=dict(type='bool', default=False),
moid=dict(type='str'),
folder=dict(type='str'),
datacenter=dict(type='str', required=True),
datacenter=dict(type='str'),
tags=dict(type='bool', default=False),
schema=dict(type='str', choices=['summary', 'vsphere'], default='summary'),
properties=dict(type='list', elements='str'),
Expand Down
34 changes: 16 additions & 18 deletions tests/integration/targets/vmware_vmotion/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@
esxi_password: '{{ esxi_password }}'
folder: '/DC0/host'
state: present
with_items: "{{ esxi_hosts }}"
with_items: '{{ esxi_hosts }}'

- name: Disable the Maintenance Mode
vmware_maintenancemode:
esxi_hostname: '{{ item }}'
state: absent
with_items: "{{ esxi_hosts }}"
with_items: '{{ esxi_hosts }}'

- name: Create VM
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ dc1 }}"
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: '{{ dc1 }}'
validate_certs: false
name: test_vm1
folder: vm
esxi_hostname: "{{ esxi1 }}"
esxi_hostname: '{{ esxi1 }}'
state: present
guest_id: debian8_64Guest
disk:
- size_gb: 1
type: thin
datastore: '{{ rw_datastore }}'
- size_gb: 1
type: thin
datastore: '{{ rw_datastore }}'
hardware:
version: 11
memory_mb: 1024
Expand All @@ -62,8 +62,7 @@

- assert:
that:
- vm_vmotion.msg == "Failed to find the virtual machine with not_a_thing"

- vm_vmotion.msg == "Failed to find the virtual machine with not_a_thing"

- name: Perform vMotion of virtual machine
vmware_vmotion:
Expand All @@ -79,8 +78,7 @@
- name: assert that changes were made
assert:
that:
- vm_vmotion is changed

- vm_vmotion is changed

- name: Add ESXi Hosts to a cluster
vmware_host:
Expand All @@ -90,13 +88,13 @@
esxi_username: '{{ esxi_user }}'
esxi_password: '{{ esxi_password }}'
state: present
with_items: "{{ esxi_hosts }}"
with_items: '{{ esxi_hosts }}'

- name: Disable the Maintenance Mode
vmware_maintenancemode:
esxi_hostname: '{{ item }}'
state: absent
with_items: "{{ esxi_hosts }}"
with_items: '{{ esxi_hosts }}'

- name: Perform vMotion of virtual machine to resource_pool
vmware_vmotion:
Expand All @@ -112,7 +110,7 @@
- name: assert that changes were made
assert:
that:
- vm_vmotion_to_rp is changed
- vm_vmotion_to_rp is changed

- name: Perform storage vMotion of virtual machine
vmware_vmotion:
Expand All @@ -128,4 +126,4 @@
- name: assert that changes were made
assert:
that:
- vm_vmotion_to_datastore is changed
- vm_vmotion_to_datastore is changed

0 comments on commit 5bca41c

Please sign in to comment.