Repository: ansible/ansible

Issue:
vmware_guest tries to customize the VM when setting start_connected
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->
vmware_guest should not attempt customizations for "start_connected" Parameter.
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
vmware_guest

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.4
  config file = /home/myuser/ansible/ansible.cfg
  configured module search path = [u'/home/myuser/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->
DEFAULT_HOST_LIST(/home/myuser/ansible/ansible.cfg) = [u'/home/myuser/ansible/hosts']
DEFAULT_ROLES_PATH(/home/myuser/ansible/ansible.cfg) = [u'/home/myuser/ansible']
DEPRECATION_WARNINGS(/home/myuser/ansible/ansible.cfg) = False
HOST_KEY_CHECKING(/home/myuser/ansible/ansible.cfg) = False
PERSISTENT_COMMAND_TIMEOUT(/home/myuser/ansible/ansible.cfg) = 40
PERSISTENT_CONNECT_TIMEOUT(/home/myuser/ansible/ansible.cfg) = 50


##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
N/A, but for documentation purposes:
CentOS Linux release 7.4.1708 (Core)
managing ESXI 6.0
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- name: Create hosts
  hosts: master
  gather_facts: no
  connection: local

  vars:
    vcenter_hostname: hostname
    esxhost: esxhost
    datastore: datastore1
    network: "VMnetwork"
    vmtemplate: mytemplate
    vmcluster: Cluster
    dumpfacts: False
    hostlist:
      - "01"

  tasks:
    - name: Create VM from template
      with_items: "{{hostlist}}"
      delegate_to: localhost
      vmware_guest:
        validate_certs: False
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"

        esxi_hostname: "{{ esxhost }}"
        datacenter: Datacenter
        folder: /hostfarm
        name: "host{{ item }}"
        template: "{{ vmtemplate }}"
        networks:
          - name: "{{ network }}"
            start_connected: True
          - name: "{{ network }}"
            start_connected: True
          - name: "{{ network }}"
            start_connected: True
        state: present

~
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
I expected ansible to create 3 NICs on my template, which has 0 currently, and then complete. "start_connected" should not require running the customization process.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Ansible created 3 new NICs, but then attempted a customization execution and errored out because IP was not defined.

*As a footnote, this issue is currently masked by https://github.com/ansible/ansible/issues/40637, but I have manually reverted the code from https://github.com/ansible/ansible/pull/38755 and now encounter this issue.
<!--- Paste verbatim command output between quotes below -->
```
TASK [Create VM from template] *******************************************************************************************************************************************************************************
failed: [1.1.1.1 -> localhost] (item=98) => {"changed": false, "item": "98", "msg": "TypeError was returned, please ensure to give correct inputs. Field \"ip\" is not optional"}
        to retry, use: --limit @/home/myuser/ansible/mkcsr.retry

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py
--- a/lib/ansible/modules/cloud/vmware/vmware_guest.py
+++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py
@@ -271,6 +271,7 @@
     - ' - C(mac) (string): Customize MAC address.'
     - ' - C(dvswitch_name) (string): Name of the distributed vSwitch.
           This value is required if multiple distributed portgroups exists with the same name. version_added 2.7'
+    - ' - C(start_connected) (bool): Indicates that virtual network adapter starts with associated virtual machine powers on. version_added: 2.5'
     - 'Optional parameters per entry (used for OS customization):'
     - ' - C(type) (string): Type of IP assignment (either C(dhcp) or C(static)). C(dhcp) is default.'
     - ' - C(ip) (string): Static IP address (implies C(type: static)).'
@@ -279,7 +280,6 @@
     - ' - C(dns_servers) (string): DNS servers for this network interface (Windows).'
     - ' - C(domain) (string): Domain name for this network interface (Windows).'
     - ' - C(wake_on_lan) (bool): Indicates if wake-on-LAN is enabled on this virtual network adapter. version_added: 2.5'
-    - ' - C(start_connected) (bool): Indicates that virtual network adapter starts with associated virtual machine powers on. version_added: 2.5'
     - ' - C(allow_guest_control) (bool): Enables guest control over whether the connectable device is connected. version_added: 2.5'
     version_added: '2.3'
   customization:
@@ -2015,7 +2015,7 @@ def deploy_vm(self):
         for nw in self.params['networks']:
             for key in nw:
                 # We don't need customizations for these keys
-                if key not in ('device_type', 'mac', 'name', 'vlan', 'type'):
+                if key not in ('device_type', 'mac', 'name', 'vlan', 'type', 'start_connected'):
                     network_changes = True
                     break
 


Test patch: