Repository: ansible/ansible

Issue:
VMware: vmware_guest_facts does not report IP address of F5 Big IP load balancer
<!---
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_facts does not report IP address of an F5 Big IP load balancer.
##### 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_facts

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.1
  config file = None
  configured module search path = [u'/home/[redacted]/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.13 (default, Nov 24 2017, 17:33:09) [GCC 6.3.0 20170516]
```

##### 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).-->

##### 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

##### 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 -->
```
ansible localhost -m vmware_guest_facts -a "datacenter=datacenter01 name=bigip-test hostname=vcenter01 username=user@vsphere.local password=secret_password validate_certs=no"

```

<!--- 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 would expect the result to mirror what vmware-tools  is reporting to vcenter. I took a peak at the vmware module_utils code that gathers the facts and I see it is going through the hw nics but with the F5s, they have a virtual interface internally that this will not pick up.
```
localhost | SUCCESS => {
    "changed": false,
    "instance": {
        "annotation": "BIG-IP Local Traffic Manager Virtual Edition\n    Copyright 2009-2016 F5 Networks (http://www.f5.com)\n    \n    For support please visit http://support.f5.com\n          ",
        "current_snapshot": null,
        "customvalues": {},
        "guest_consolidation_needed": false,
        "guest_question": null,
        "guest_tools_status": "guestToolsRunning",
        "guest_tools_version": "2147483647",
        "hw_cores_per_socket": 1,
        "hw_datastores": [
            "vmware"
        ],
        "hw_esxi_host": "esx01.internal.homelab.systems",
        "hw_eth0": {
            "addresstype": "assigned",
            "ipaddresses": null,
            "label": "Network adapter 1",
            "macaddress": "00:50:56:8c:18:13",
            "macaddress_dash": "00-50-56-8c-18-13",
            "portgroup_key": null,
            "portgroup_portkey": null,
            "summary": "VM Network"
        },
        "hw_eth1": {
            "addresstype": "assigned",
            "ipaddresses": null,
            "label": "Network adapter 2",
            "macaddress": "00:50:56:8c:b2:1f",
            "macaddress_dash": "00-50-56-8c-b2-1f",
            "portgroup_key": null,
            "portgroup_portkey": null,
            "summary": "VM Network"
        },
        "hw_eth2": {
            "addresstype": "assigned",
            "ipaddresses": null,
            "label": "Network adapter 3",
            "macaddress": "00:50:56:8c:18:1b",
            "macaddress_dash": "00-50-56-8c-18-1b",
            "portgroup_key": null,
            "portgroup_portkey": null,
            "summary": "VM Network"
        },
        "hw_eth3": {
            "addresstype": "assigned",
            "ipaddresses": null,
            "label": "Network adapter 4",
            "macaddress": "00:50:56:8c:70:43",
            "macaddress_dash": "00-50-56-8c-70-43",
            "portgroup_key": null,
            "portgroup_portkey": null,
            "summary": "VM Network"
        },
        "hw_files": [
            "[vmware] bigip-test/bigip-test.vmx",
            "[vmware] bigip-test/bigip-test.vmsd",
            "[vmware] bigip-test/bigip-test.nvram",
            "[vmware] bigip-test/bigip-test.vmxf",
            "[vmware] bigip-test/bigip-test.vmdk",
            "[vmware] bigip-test/bigip-test_1.vmdk"
        ],
        "hw_folder": "/datacenter01/vm",
        "hw_guest_full_name": "CentOS 4/5 or later (64-bit)",
        "hw_guest_ha_state": true,
        "hw_guest_id": "centos64Guest",
        "hw_interfaces": [
            "eth0",
            "eth1",
            "eth2",
            "eth3"
        ],
        "hw_is_template": false,
        "hw_memtotal_mb": 4096,
        "hw_name": "bigip-test",
        "hw_power_status": "poweredOn",
        "hw_processor_count": 2,
        "hw_product_uuid": "420c0cd7-fd8f-48ec-7646-c8e5dad64374",
        "hw_version": "vmx-10",
        "instance_uuid": "500cd4c2-f848-85d5-d270-bc60f5dcabda",
        "ipv4": "10.4.3.25",
        "ipv6": null,
        "module_hw": true,
        "snapshots": []
    }
}
```
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
localhost | SUCCESS => {
    "changed": false,
    "instance": {
        "annotation": "BIG-IP Local Traffic Manager Virtual Edition\n    Copyright 2009-2016 F5 Networks (http://www.f5.com)\n    \n    For support please visit http://support.f5.com\n          ",
        "current_snapshot": null,
        "customvalues": {},
        "guest_consolidation_needed": false,
        "guest_question": null,
        "guest_tools_status": "guestToolsRunning",
        "guest_tools_version": "2147483647",
        "hw_cores_per_socket": 1,
        "hw_datastores": [
            "vmware"
        ],
        "hw_esxi_host": "esx01.internal.homelab.systems",
        "hw_eth0": {
            "addresstype": "assigned",
            "ipaddresses": null,
            "label": "Network adapter 1",
            "macaddress": "00:50:56:8c:18:13",
            "macaddress_dash": "00-50-56-8c-18-13",
            "portgroup_key": null,
            "portgroup_portkey": null,
            "summary": "VM Network"
        },
        "hw_eth1": {
            "addresstype": "assigned",
            "ipaddresses": null,
            "label": "Network adapter 2",
            "macaddress": "00:50:56:8c:b2:1f",
            "macaddress_dash": "00-50-56-8c-b2-1f",
            "portgroup_key": null,
            "portgroup_portkey": null,
            "summary": "VM Network"
        },
        "hw_eth2": {
            "addresstype": "assigned",
            "ipaddresses": null,
            "label": "Network adapter 3",
            "macaddress": "00:50:56:8c:18:1b",
            "macaddress_dash": "00-50-56-8c-18-1b",
            "portgroup_key": null,
            "portgroup_portkey": null,
            "summary": "VM Network"
        },
        "hw_eth3": {
            "addresstype": "assigned",
            "ipaddresses": null,
            "label": "Network adapter 4",
            "macaddress": "00:50:56:8c:70:43",
            "macaddress_dash": "00-50-56-8c-70-43",
            "portgroup_key": null,
            "portgroup_portkey": null,
            "summary": "VM Network"
        },
        "hw_files": [
            "[vmware] bigip-test/bigip-test.vmx",
            "[vmware] bigip-test/bigip-test.vmsd",
            "[vmware] bigip-test/bigip-test.nvram",
            "[vmware] bigip-test/bigip-test.vmxf",
            "[vmware] bigip-test/bigip-test.vmdk",
            "[vmware] bigip-test/bigip-test_1.vmdk"
        ],
        "hw_folder": "/datacenter01/vm",
        "hw_guest_full_name": "CentOS 4/5 or later (64-bit)",
        "hw_guest_ha_state": true,
        "hw_guest_id": "centos64Guest",
        "hw_interfaces": [
            "eth0",
            "eth1",
            "eth2",
            "eth3"
        ],
        "hw_is_template": false,
        "hw_memtotal_mb": 4096,
        "hw_name": "bigip-test",
        "hw_power_status": "poweredOn",
        "hw_processor_count": 2,
        "hw_product_uuid": "420c0cd7-fd8f-48ec-7646-c8e5dad64374",
        "hw_version": "vmx-10",
        "instance_uuid": "500cd4c2-f848-85d5-d270-bc60f5dcabda",
        "ipv4": null,
        "ipv6": null,
        "module_hw": true,
        "snapshots": []
    }
}
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/module_utils/vmware.py b/lib/ansible/module_utils/vmware.py
--- a/lib/ansible/module_utils/vmware.py
+++ b/lib/ansible/module_utils/vmware.py
@@ -354,13 +354,11 @@ def gather_vm_facts(content, vm):
         for device in vmnet:
             net_dict[device.macAddress] = list(device.ipAddress)
 
-    for dummy, v in iteritems(net_dict):
-        for ipaddress in v:
-            if ipaddress:
-                if '::' in ipaddress:
-                    facts['ipv6'] = ipaddress
-                else:
-                    facts['ipv4'] = ipaddress
+    if vm.guest.ipAddress:
+        if ':' in vm.guest.ipAddress:
+            facts['ipv6'] = vm.guest.ipAddress
+        else:
+            facts['ipv4'] = vm.guest.ipAddress
 
     ethernet_idx = 0
     for entry in vm.config.hardware.device:


Test patch: