Repository: ansible-collections/community.vmware

Issue:
vmware_guest_network: fails with a backtrace if static MAC starts with '00:50:56'
##### SUMMARY
<!--- Explain the problem briefly below -->
hi @pgbidkar and @Tomorrow9.

If I use the following task:

```yaml
  - name: add new network adapters to virtual machine
    vmware_guest_network:
      validate_certs: False
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      name: test_vm1
      networks:
        - name: "VM Network"
          state: new
          device_type: e1000e
          manual_mac: "00:50:56:58:59:60"
        - name: "VM Network"
          state: new
          device_type: vmxnet3
          manual_mac: "00:50:56:58:59:61"
    register: add_netadapter
```
I get the following error: [add_nic.txt](https://github.com/ansible/ansible/files/3709181/add_nic.txt)

If I remove the `manual_mac` keys, everything works fine.

I create the Fedora 30 VM with:

```yaml
  - name: Create VMs
    vmware_guest:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      datacenter: "{{ dc1 }}"
      validate_certs: no
      folder: '/DC0/vm/F0'
      name: test_vm1
      state: poweredon
      guest_id: centos7_64Guest
      disk:
      - size_gb: 1
        type: thin
        datastore: '{{ ds2 }}'
      hardware:
        version: latest
        memory_mb: 1024
        num_cpus: 1
        scsi: paravirtual
      cdrom:
        type: iso
        iso_path: "[{{ ds1 }}] fedora.iso"
      networks:
      - name: VM Network
```
I've tried several hardware.version/guest_id combination, without any difference. My lab is set-up as described here: https://docs.ansible.com/ansible/devel/dev_guide/platforms/vmware_guidelines.html

My vcenter set-up:

```shell
$ govc ls '/**/**/**'
/DC0/vm/F0/test_vm1
/DC0/network/VM Network
/DC0/host/DC0_C0/Resources
/DC0/host/DC0_C0/esxi1.test
/DC0/host/DC0_C0/esxi2.test
/DC0/datastore/LocalDS_0
/DC0/datastore/LocalDS_1
/DC0/datastore/datastore1 (1)
/DC0/datastore/datastore1
```

##### 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_network

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
devel
```



Discussion:
@goneri said - <br> It's not a problem with the module. I get exactly the same error if I the same operation manually using vcenter web UI. 
@goneri said - <br> Ah! The problem comes from the 00:50:56 prefix (VMware), If I use another one, everything just works as expected! 
@goneri said - <br> https://github.com/ansible/ansible/issues/51221 seems to be the the same problem. MAC starts with 00:50:56 too. 
Migrated from https://github.com/ansible/ansible/issues/63302 to this repo
cc @goneri
@Akasurde, yeah, I can reproduce the issue using "vmware_guest_network" module with MAC address prefix "00:50:56", but not reproduce on web UI on vSphere 6.7U3 manually even set with the same MAC address as the existing network adapter. I'm not quite sure about if "VMware Organizationally Unique Identifier (OUI) 00:50:56" can be set manually, but I'll confirm that later. Thanks.

cc @lparkes @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
For me this behavior is bit different. I am using `vmware_guest` ansible module to deploy multiple vms from a template and in the ansible playbook, I have kept the random MAC calculation logic. Out of two vms, one if working fine but other one is failing with the following error.

![image](https://user-images.githubusercontent.com/1809177/145823937-55e92457-e632-4f4f-89ad-764df783654c.png)

My ansible role is here:

```
---
- name: Deploying vm from '{{ win_temp }}'
  vmware_guest:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    datacenter: '{{ vsphere_datacenter }}'
    cluster: '{{ vsphere_cluster }}'
    datastore: '{{ vsphere_datastore }}'
    name: '{{ inventory_hostname }}'
    template: '{{ win_temp }}'
    folder: '{{ folder }}'
    validate_certs: 'no'
    networks:
    - name: '{{ Mgmt_network }}'
      ip: "{{ Mgmt_network_ipv4 }}"
      netmask: '{{ Mgmt_network_nmv4 }}'
      mac: "{{ '00:50:56' | community.general.random_mac(seed=inventory_hostname) }}"
      gateway: '{{ Mgmt_network_gwv4 }}'
      dns_servers:
        - '{{ dns_server1 }}'
        - '{{ dns_server2 }}'
    state: poweredon
    wait_for_ip_address: yes
    customization:
      hostname: "{{ vsphere_vm_hostname }}"
      dns_suffix: '{{ ad_domain }}'
      domainadmin: '{{ ad_domain_admin }}'
      domainadminpassword: '{{ ad_domain_password }}'
      joindomain: '{{ ad_domain }}'
      timezone: '{{ timezone }}'
    wait_for_customization: yes
  delegate_to: localhost
```

I am not sure who is using that MAC address as mentioned in the error message.

As per VMware documentation, its reserved for some ESXi work. Refer https://docs.vmware.com/en/VMware-vSphere/6.5/com.vmware.vsphere.troubleshooting.doc/GUID-7F723748-E7B8-48B9-A773-3822C514684B.html 

Resolving patch:
diff --git a/plugins/modules/vmware_guest_network.py b/plugins/modules/vmware_guest_network.py
--- a/plugins/modules/vmware_guest_network.py
+++ b/plugins/modules/vmware_guest_network.py
@@ -184,6 +184,7 @@
         - Manual specified MAC address of the network adapter when creating, or reconfiguring.
         - If not specified when creating new network adapter, mac address will be generated automatically.
         - When reconfigure MAC address, VM should be in powered off state.
+        - There are restrictions on the MAC addresses you can set. Consult the documentation of your vSphere version as to allowed MAC addresses.
       connected:
         type: bool
         description:
@@ -330,8 +331,9 @@
     pass
 
 import copy
+from ansible.module_utils._text import to_native
 from ansible.module_utils.basic import AnsibleModule
-from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, vmware_argument_spec, wait_for_task
+from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, TaskError, vmware_argument_spec, wait_for_task
 
 
 class PyVmomiHelper(PyVmomi):
@@ -792,6 +794,8 @@ def _nic_present(self, network_params=None):
                 wait_for_task(task)
             except (vim.fault.InvalidDeviceSpec, vim.fault.RestrictedVersion) as e:
                 self.module.fail_json(msg='failed to reconfigure guest', detail=e.msg)
+            except TaskError as task_e:
+                self.module.fail_json(msg=to_native(task_e))
 
             if task.info.state == 'error':
                 self.module.fail_json(msg='failed to reconfigure guest', detail=task.info.error.msg)


Test patch: