diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index d405faf85e3..c608449a2ad 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -1369,28 +1369,45 @@ class PyVmomiHelper(PyVmomi): def get_configured_disk_size(self, expected_disk_spec): # what size is it? if [x for x in expected_disk_spec.keys() if x.startswith('size_') or x == 'size']: - # size_tb, size_gb, size_mb, size_kb, size_b ...? + # size, size_tb, size_gb, size_mb, size_kb if 'size' in expected_disk_spec: - expected = ''.join(c for c in expected_disk_spec['size'] if c.isdigit()) - unit = expected_disk_spec['size'].replace(expected, '').lower() - expected = int(expected) + size_regex = re.compile(r'(\d+(?:\.\d+)?)([tgmkTGMK][bB])') + disk_size_m = size_regex.match(expected_disk_spec['size']) + try: + if disk_size_m: + expected = disk_size_m.group(1) + unit = disk_size_m.group(2) + else: + raise ValueError + + if re.match(r'\d+\.\d+', expected): + # We found float value in string, let's typecast it + expected = float(expected) + else: + # We found int value in string, let's typecast it + expected = int(expected) + + if not expected or not unit: + raise ValueError + + except (TypeError, ValueError, NameError): + # Common failure + self.module.fail_json(msg="Failed to parse disk size please review value" + " provided using documentation.") else: param = [x for x in expected_disk_spec.keys() if x.startswith('size_')][0] unit = param.split('_')[-1].lower() expected = [x[1] for x in expected_disk_spec.items() if x[0].startswith('size_')][0] expected = int(expected) - if unit == 'tb': - return expected * 1024 * 1024 * 1024 - elif unit == 'gb': - return expected * 1024 * 1024 - elif unit == 'mb': - return expected * 1024 - elif unit == 'kb': - return expected - - self.module.fail_json( - msg='%s is not a supported unit for disk size. Supported units are kb, mb, gb or tb' % unit) + disk_units = dict(tb=3, gb=2, mb=1, kb=0) + if unit in disk_units: + unit = unit.lower() + return expected * (1024 ** disk_units[unit]) + else: + self.module.fail_json(msg="%s is not a supported unit for disk size." + " Supported units are ['%s']." % (unit, + "', '".join(disk_units.keys()))) # No size found but disk, fail self.module.fail_json( diff --git a/test/integration/targets/vmware_guest/tasks/clone_d1_c1_f0.yml b/test/integration/targets/vmware_guest/tasks/clone_d1_c1_f0.yml index 53603ef6f93..57640e3d213 100644 --- a/test/integration/targets/vmware_guest/tasks/clone_d1_c1_f0.yml +++ b/test/integration/targets/vmware_guest/tasks/clone_d1_c1_f0.yml @@ -40,7 +40,7 @@ template: "{{ item|basename }}" datacenter: "{{ (item|basename).split('_')[0] }}" disk: - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True state: poweredoff diff --git a/test/integration/targets/vmware_guest/tasks/create_d1_c1_f0.yml b/test/integration/targets/vmware_guest/tasks/create_d1_c1_f0.yml index 259a24db670..5f7f68dbf52 100644 --- a/test/integration/targets/vmware_guest/tasks/create_d1_c1_f0.yml +++ b/test/integration/targets/vmware_guest/tasks/create_d1_c1_f0.yml @@ -58,7 +58,7 @@ # cpu_reservation: 4096 max_connections: 10 disk: - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True state: poweredoff @@ -88,7 +88,7 @@ num_cpu_cores_per_socket: 2 memory_mb: 512 disk: - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True state: poweredoff diff --git a/test/integration/targets/vmware_guest/tasks/create_nw_d1_c1_f0.yml b/test/integration/targets/vmware_guest/tasks/create_nw_d1_c1_f0.yml index 1adc731521b..3c8b1687557 100644 --- a/test/integration/targets/vmware_guest/tasks/create_nw_d1_c1_f0.yml +++ b/test/integration/targets/vmware_guest/tasks/create_nw_d1_c1_f0.yml @@ -43,7 +43,7 @@ num_cpus: 4 memory_mb: 512 disk: - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True networks: diff --git a/test/integration/targets/vmware_guest/tasks/create_rp_d1_c1_f0.yml b/test/integration/targets/vmware_guest/tasks/create_rp_d1_c1_f0.yml index 161cd0a92ad..7a89cc1675b 100644 --- a/test/integration/targets/vmware_guest/tasks/create_rp_d1_c1_f0.yml +++ b/test/integration/targets/vmware_guest/tasks/create_rp_d1_c1_f0.yml @@ -59,7 +59,7 @@ num_cpus: 1 memory_mb: 512 disk: - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True state: poweredoff @@ -112,7 +112,7 @@ num_cpus: 1 memory_mb: 512 disk: - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True state: poweredoff @@ -167,7 +167,7 @@ num_cpus: 1 memory_mb: 512 disk: - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True state: poweredoff @@ -221,7 +221,7 @@ num_cpus: 1 memory_mb: 512 disk: - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True state: poweredoff diff --git a/test/integration/targets/vmware_guest/tasks/disk_size_d1_c1_f0.yml b/test/integration/targets/vmware_guest/tasks/disk_size_d1_c1_f0.yml new file mode 100644 index 00000000000..e824a2c8f1c --- /dev/null +++ b/test/integration/targets/vmware_guest/tasks/disk_size_d1_c1_f0.yml @@ -0,0 +1,60 @@ +# Test code for the vmware_guest module. +# Copyright: (c) 2018, Abhijeet Kasurde +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +- name: Wait for Flask controller to come up online + wait_for: + host: "{{ vcsim }}" + port: 5000 + state: started + +- name: kill vcsim + uri: + url: http://{{ vcsim }}:5000/killall +- name: start vcsim with no folders + uri: + url: http://{{ vcsim }}:5000/spawn?datacenter=1&cluster=1&folder=0 + register: vcsim_instance + +- name: Wait for Flask controller to come up online + wait_for: + host: "{{ vcsim }}" + port: 443 + state: started + +- name: get a list of VMS from vcsim + uri: + url: http://{{ vcsim }}:5000/govc_find?filter=VM + register: vmlist + +- debug: var=vcsim_instance +- debug: var=vmlist + +- name: create new VMs with invalid disk size + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ 'newvm_' + item|basename }}" + guest_id: centos64Guest + datacenter: "{{ (item|basename).split('_')[0] }}" + hardware: + num_cpus: 1 + memory_mb: 512 + disk: + - size: 0gb + type: eagerzeroedthick + autoselect_datastore: True + state: poweredoff + folder: "{{ item|dirname }}" + with_items: "{{ vmlist['json'] }}" + register: disk_size_d1_c1_f0 + ignore_errors: True + +- debug: var=disk_size_d1_c1_f0 + +- name: assert that changes were made + assert: + that: + - "disk_size_d1_c1_f0.results|map(attribute='changed')|unique|list == [false]" diff --git a/test/integration/targets/vmware_guest/tasks/disk_type_d1_c1_f0.yml b/test/integration/targets/vmware_guest/tasks/disk_type_d1_c1_f0.yml index 47c435c4903..db613f3d10a 100644 --- a/test/integration/targets/vmware_guest/tasks/disk_type_d1_c1_f0.yml +++ b/test/integration/targets/vmware_guest/tasks/disk_type_d1_c1_f0.yml @@ -43,10 +43,10 @@ num_cpus: 1 memory_mb: 512 disk: - - size: 0gb + - size: 1gb type: eagerzeroedthick autoselect_datastore: True - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True state: poweredoff diff --git a/test/integration/targets/vmware_guest/tasks/mac_address_d1_c1_f0.yml b/test/integration/targets/vmware_guest/tasks/mac_address_d1_c1_f0.yml index 5385cb9033d..1aa75a0287f 100644 --- a/test/integration/targets/vmware_guest/tasks/mac_address_d1_c1_f0.yml +++ b/test/integration/targets/vmware_guest/tasks/mac_address_d1_c1_f0.yml @@ -37,7 +37,7 @@ num_cpus: 1 memory_mb: 512 disk: - - size: 0gb + - size: 1gb type: thin autoselect_datastore: True networks: diff --git a/test/integration/targets/vmware_guest/tasks/main.yml b/test/integration/targets/vmware_guest/tasks/main.yml index 9ab97098a0c..b8dc228565d 100644 --- a/test/integration/targets/vmware_guest/tasks/main.yml +++ b/test/integration/targets/vmware_guest/tasks/main.yml @@ -31,3 +31,4 @@ #- include: network_with_dvpg.yml #- include: template_d1_c1_f0.yml - include: vapp_d1_c1_f0.yml +- include: disk_size_d1_c1_f0.yml