##### SUMMARY

The following snippet from https://github.com/ansible/ansible/blob/devel/test/integration/targets/vmware_guest/tasks/create_d1_c1_f0.yml uses `autoselect_datastore: True`.

```yaml
- name: create new VMs
  vmware_guest:
    validate_certs: False
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    name: newvm_1
    #template: "{{ item|basename }}"
    guest_id: centos64Guest
    datacenter: "{{ dc1 }}"
    hardware:
        num_cpus: 1
        num_cpu_cores_per_socket: 1
        memory_mb: 256
        hotadd_memory: true
        hotadd_cpu: false
        max_connections: 10
    disk:
        - size: 1gb
          type: thin
          autoselect_datastore: True
    state: poweredoff
    folder: F0
```

One of my two datastores is dedicated to the ISO image and so, is readOnly. But it still get selected consistently. I use the following hack to avoid the problem:

```diff
diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py
index 6a63e97798..3648e3e87f 100644
--- a/lib/ansible/modules/cloud/vmware/vmware_guest.py
+++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py
@@ -1925,6 +1925,15 @@ class PyVmomiHelper(PyVmomi):
 
                 datastore_freespace = 0
                 for ds in datastores:
+                    is_readonly = False
+                    for h in ds.host:
+                        if h.mountInfo.accessMode == 'readOnly':
+                            is_readonly = True
+                            break
+
+                    if is_readonly:
+                        continue
+
                     if (ds.summary.freeSpace > datastore_freespace) or (ds.summary.freeSpace == datastore_freespace and not datastore):
                         # If datastore field is provided, filter destination datastores
                         if 'datastore' in self.params['disk'][0] and \
```
<!--- Explain the problem briefly below -->

##### 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

module: vmware_guest
short_description: Manages virtual machines in vCenter
description: >
   This module can be used to create new virtual machines from templates or other virtual machines,
   manage power state of virtual machine such as power on, power off, suspend, shutdown, reboot, restart etc.,
   modify various virtual machine components like network, disk, customization etc.,
   rename a virtual machine and remove a virtual machine with associated components.
version_added: '2.2'
author:
- Loic Blot (@nerzhul) <loic.blot@unix-experience.fr>
- Philippe Dellaert (@pdellaert) <philippe@dellaert.org>
- Abhijeet Kasurde (@Akasurde) <akasurde@redhat.com>
requirements:
- python >= 2.6
- PyVmomi
notes:
    - Please make sure that the user used for vmware_guest has the correct level of privileges.
    - For example, following is the list of minimum privileges required by users to create virtual machines.
    - "   DataStore > Allocate Space"
    - "   Virtual Machine > Configuration > Add New Disk"
    - "   Virtual Machine > Configuration > Add or Remove Device"
    - "   Virtual Machine > Inventory > Create New"
    - "   Network > Assign Network"
    - "   Resource > Assign Virtual Machine to Resource Pool"
    - "Module may require additional privileges as well, which may be required for gathering facts - e.g. ESXi configurations."
    - Tested on vSphere 5.5, 6.0, 6.5 and 6.7
    - Use SCSI disks instead of IDE when you want to expand online disks by specifing a SCSI controller
    - "For additional information please visit Ansible VMware community wiki - U(https://github.com/ansible/community/wiki/VMware)."
options:
  state: