Repository: ansible/ansible

Issue:
vmware_guest: Clone from template results in fault.NicSettingMismatch.summary
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
vmware_guest

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
v2.3

##### SUMMARY
<!--- Explain the problem briefly -->
Spending a long time understanding what is going on, but the fact that it takes about 10 minutes before it fails (duing VM creation) makes it hard to troubleshoot.

The template has exactly one NIC, and my definition also has exactly one NIC, so it should be obvious that there is no real mismatch in number of NICs. But looking at other reports on the net seems to indicate a problem related to identity customizations (related to domain or DNS config).


Discussion:
cc @jctanner
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@nerzhul Have you seen this before ? I cannot wrap my head around this one :-/
did saw this error before, it's a VMWare API error ?
https://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.fault.NicSettingMismatch.html
"The number of network adapter settings in the customization specification does not match the number of network adapters present in the virtual machine."
What I noticed is that when this happens I end up with a VM with 2 NICs (the original from the template and a new one with my config). So this is definitely a regression in the logic, and I don't have to look at anything else anymore :-)

The reason this worked before was that the previous code removed all NICs and then added what was requested. (suboptimal, I agree, but it worked ;-))

I lost a few hours because of this though...
Yes old behaviour removed all NIC but it's not acceptable when using idempotence with running VM

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
@@ -420,12 +420,20 @@ def create_scsi_disk(self, scsi_ctl, disk_index=None):
 
     def create_nic(self, device_type, device_label, device_infos):
         nic = vim.vm.device.VirtualDeviceSpec()
-        if device_type == 'vmxnet3':
+        if device_type == 'pcnet32':
+            nic.device = vim.vm.device.VirtualPCNet32()
+        if device_type == 'vmxnet2':
+            nic.device = vim.vm.device.VirtualVmxnet2()
+        elif device_type == 'vmxnet3':
             nic.device = vim.vm.device.VirtualVmxnet3()
         elif device_type == 'e1000':
             nic.device = vim.vm.device.VirtualE1000()
+        elif device_type == 'e1000e':
+            nic.device = vim.vm.device.VirtualE1000e()
+        elif device_type == 'sriov':
+            nic.device = vim.vm.device.VirtualSriovEthernetCard()
         else:
-            self.module.fail_json(msg="invalid device_type '%s' for network %s" %
+            self.module.fail_json(msg="Invalid device_type '%s' for network %s" %
                                       (device_type, device_infos['network']))
 
         nic.device.wakeOnLanEnabled = True
@@ -828,7 +836,12 @@ def get_vm_network_interfaces(self, vm=None):
 
         device_list = []
         for device in vm.config.hardware.device:
-            if isinstance(device, vim.vm.device.VirtualVmxnet3) or isinstance(device, vim.vm.device.VirtualE1000):
+            if isinstance(device, vim.vm.device.VirtualPCNet32) or \
+               isinstance(device, vim.vm.device.VirtualVmxnet2) or \
+               isinstance(device, vim.vm.device.VirtualVmxnet3) or \
+               isinstance(device, vim.vm.device.VirtualE1000) or \
+               isinstance(device, vim.vm.device.VirtualE1000e) or \
+               isinstance(device, vim.vm.device.VirtualSriovEthernetCard):
                 device_list.append(device)
 
         return device_list


Test patch: