Repository: ansible/ansible

Issue:
vmware_guest cannot connect a VM to NSX-T Logical Switch

##### ISSUE TYPE
 - Bug Report
 
##### COMPONENT NAME
vmware_guest


##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0 (devel 2d0547c411) last updated 2018/03/26 23:15:52 (GMT +200)
  config file = None
  configured module search path = ['/Users/ysimeonov/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/ysimeonov/ansible_clone_test/ansible/lib/ansible
  executable location = /Users/ysimeonov/ansible_clone_test/ansible/bin/ansible
  python version = 3.6.4 (default, Mar  1 2018, 18:36:50) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
```

##### CONFIGURATION
clone for devel branch with no changes

##### OS / ENVIRONMENT
ANY

##### SUMMARY
When we clone a VM using vmware_guest module and trying to connect it to a NSX-T Logical Switch providing the Logical Switch name it creates a standard portgroup with the same name instead of connecting it to the existing Logical Switch. This is because the code checks if it is distributed portgroup and if it is not, it assumes it is a standard port group.

There are three types of networks in vSphere. 
- Standard vSwitch Portgroup
- Distributed vSwitch Portgroup
- NSX-T Logical Switch

The module doesn't take a proper action if we tries to connect to NSX-T Logical Switch. 

I will fix it and submit a pull request later today! 

##### STEPS TO REPRODUCE
Specify a NSX-T logical Switch name in networks: name

```yaml
---
- name: Test NSX-T Logical Switch
  hosts: localhost
  connection: local
  gather_facts: False
  tasks:
    - name: Create a VM from a template
      vmware_guest:
        hostname: 10.29.18.43
        username: administrator@yasen.local
        password: VMware1!
        validate_certs: no
        folder: /VRM
        name: testvm2
        cluster: T_Cluster
        datacenter: MyDC1
        state: poweredon
        template: t_template_novra
        disk:
        - size_gb: 16
          type: thin
          datastore: NFS
        hardware:
          memory_mb: 1024
          num_cpus: 1
          scsi: paravirtual
        networks:
        - name: testLS
          ip: 192.168.10.20
          netmask: 255.255.255.0
          gateway: 192.168.10.1
          domain: yasen.local
          dns_servers:
          - 10.29.12.201
        wait_for_ip_address: no
      delegate_to: localhost
      register: deploy

```

##### EXPECTED RESULTS
To connect the cloned VM to the existing NSX-T Logical Switch with name testLS.

##### ACTUAL RESULTS
The playbook completes successfully but it creates a new standard switch portgroup with name testLS and connects the VM to it. The interface is also in a bad (down) state and cannot bring it up even manually.


Discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/vmware/vmware_guest.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/vmware/vmware_guest.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Akasurde @akasurde @dagwieers @dav1x @jctanner @nerzhul @pdellaert
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

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
@@ -1116,6 +1116,14 @@ def configure_network(self, vm_obj):
                 dvs_port_connection.switchUuid = pg_obj.config.distributedVirtualSwitch.uuid
                 nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo()
                 nic.device.backing.port = dvs_port_connection
+
+            elif isinstance(self.cache.get_network(network_name), vim.OpaqueNetwork):
+                # NSX-T Logical Switch
+                nic.device.backing = vim.vm.device.VirtualEthernetCard.OpaqueNetworkBackingInfo()
+                nic.device.backing.opaqueNetworkType = 'nsx.LogicalSwitch'
+                nic.device.backing.opaqueNetworkId = self.cache.get_network(network_name).summary.opaqueNetworkId
+                nic.device.deviceInfo.summary = 'nsx.LogicalSwitch: %s' % (self.cache.get_network(network_name).summary.opaqueNetworkId)
+
             else:
                 # vSwitch
                 if not isinstance(nic.device.backing, vim.vm.device.VirtualEthernetCard.NetworkBackingInfo):


Test patch: