Repository: ansible-collections/community.vmware

Issue:
vmware_guest: parameter like controller_type or unit_number should convert to int firstly
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
Parameters' values with "int" type should be converted to int firstly using "integer_value" function. Or it will pass string type of value when setting it to a variable.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
vmware_guest

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below

```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below

```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->


##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: "Reconfigure VM CDROM"
  vmware_guest:
    hostname: "{{ vc }}"
    username: "{{ vc_user }}"
    password: "{{ vc_password }}"
    validate_certs: "{{ valid_cert }}"
    datacenter: "{{ vc_datacenter }}"
    folder: "{{ vm_folder }}"
    name: "{{ vm_name }}"
    cdrom:
      - type: "{{ cdrom_type }}"
        iso_path: "{{ cdrom_iso_file | default(omit) }}"
        controller_type: "{{ cdrom_controller | default(omit) }}"
        controller_number: "{{ cdrom_controller_num }}"
        unit_number: "{{ cdrom_unit_num }}"
        state: "present"
  register: vm_result
```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
When int type parameter's value is set to other variable, and this variable's value is not set to an integer directly, then passed parameter's value is treated as string type. So in the code, we need to convert it to int firstly before validate it's value. 

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->

```
TASK [Reconfigure VM CDROM] **************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Invalid cdrom.controller_number: 0 or cdrom.unit_number: 1, valid values are 0 or 1 for IDE controller."}
```

<!--- Paste verbatim command output between quotes -->
```paste below

```



Discussion:

Resolving patch:
diff --git a/plugins/modules/vmware_guest.py b/plugins/modules/vmware_guest.py
--- a/plugins/modules/vmware_guest.py
+++ b/plugins/modules/vmware_guest.py
@@ -1285,18 +1285,28 @@ def sanitize_cdrom_params(self):
                     if cdrom_spec['type'] == 'iso' and not cdrom_spec.get('iso_path'):
                         self.module.fail_json(msg="cdrom.iso_path is mandatory when cdrom.type is set to iso.")
 
-                if cdrom_spec['controller_type'] == 'ide' and \
-                        (cdrom_spec.get('controller_number') not in [0, 1] or cdrom_spec.get('unit_number') not in [0, 1]):
+                if 'controller_number' not in cdrom_spec or 'unit_number' not in cdrom_spec:
+                    self.module.fail_json(msg="'cdrom.controller_number' and 'cdrom.unit_number' are required"
+                                              " parameters when configure CDROM list.")
+                try:
+                    cdrom_ctl_num = int(cdrom_spec.get('controller_number'))
+                    cdrom_ctl_unit_num = int(cdrom_spec.get('unit_number'))
+                except ValueError:
+                    self.module.fail_json(msg="'cdrom.controller_number' and 'cdrom.unit_number' attributes should be "
+                                              "integer values.")
+
+                if cdrom_spec['controller_type'] == 'ide' and (cdrom_ctl_num not in [0, 1] or cdrom_ctl_unit_num not in [0, 1]):
                     self.module.fail_json(msg="Invalid cdrom.controller_number: %s or cdrom.unit_number: %s, valid"
                                               " values are 0 or 1 for IDE controller."
                                               % (cdrom_spec.get('controller_number'), cdrom_spec.get('unit_number')))
 
-                if cdrom_spec['controller_type'] == 'sata' and \
-                        (cdrom_spec.get('controller_number') not in range(0, 4) or cdrom_spec.get('unit_number') not in range(0, 30)):
+                if cdrom_spec['controller_type'] == 'sata' and (cdrom_ctl_num not in range(0, 4) or cdrom_ctl_unit_num not in range(0, 30)):
                     self.module.fail_json(msg="Invalid cdrom.controller_number: %s or cdrom.unit_number: %s,"
                                               " valid controller_number value is 0-3, valid unit_number is 0-29"
                                               " for SATA controller." % (cdrom_spec.get('controller_number'),
                                                                          cdrom_spec.get('unit_number')))
+                cdrom_spec['controller_number'] = cdrom_ctl_num
+                cdrom_spec['unit_number'] = cdrom_ctl_unit_num
 
                 ctl_exist = False
                 for exist_spec in cdrom_specs:
@@ -2191,8 +2201,11 @@ def sanitize_disk_parameters(self, vm_obj):
                                           " mandatory parameters when configure multiple disk controllers and disks.")
             try:
                 ctl_num = int(disk_spec['controller_number'])
+                ctl_unit_num = int(disk_spec['unit_number'])
             except ValueError:
-                self.module.fail_json(msg="Failed to parse 'disk.controller_number' value, valid type is integer.")
+                self.module.fail_json(msg="'disk.controller_number' and 'disk.unit_number' attributes should be integer"
+                                          " values.")
+            disk_spec['unit_number'] = ctl_unit_num
             ctl_type = disk_spec['controller_type'].lower()
             # max number of same type disk controller is 4
             if ctl_num > 3:


Test patch: