Repository: ansible/ansible

Issue:
vmware_inventory pyVmomi.VmomiSupport.NoPermission
<!---
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_inventory](/ansible/ansible/blob/devel/contrib/inventory/vmware_inventory.py)

##### SUMMARY
<!--- Explain the problem briefly -->

```./vmware_inventory.py``` fails:

```
Traceback (most recent call last):
  File "./vmware_inventory.py", line 721, in <module>
    print(VMWareInventory().show())
  File "./vmware_inventory.py", line 134, in __init__
    self.do_api_calls_update_cache()
  File "./vmware_inventory.py", line 176, in do_api_calls_update_cache
    self.inventory = self.instances_to_inventory(self.get_instances())
  File "./vmware_inventory.py", line 329, in get_instances
    return self._get_instances(kwargs)
  File "./vmware_inventory.py", line 369, in _get_instances
    ifacts = self.facts_from_vobj(instance)
  File "./vmware_inventory.py", line 614, in facts_from_vobj
    inkey=method,
  File "./vmware_inventory.py", line 692, in _process_object_types
    level=(level + 1)
  File "./vmware_inventory.py", line 631, in _process_object_types
    rdata[key] = getattr(vobj, key)
  File "/home/user/vmware/lib/python2.7/site-packages/pyVmomi/VmomiSupport.py", line 574, in __call__
    return self.f(*args, **kwargs)
  File "/home/user/vmware/lib/python2.7/site-packages/pyVmomi/VmomiSupport.py", line 394, in _InvokeAccessor
    return self._stub.InvokeAccessor(self, info)
  File "/home/user/vmware/lib/python2.7/site-packages/pyVmomi/StubAdapterAccessorImpl.py", line 47, in InvokeAccessor
    raise objectContent.missingSet[0].fault
pyVmomi.VmomiSupport.NoPermission: (vim.fault.NoPermission) {
   dynamicType = <unset>,
   dynamicProperty = (vmodl.DynamicProperty) [],
   msg = '',
   faultCause = <unset>,
   faultMessage = (vmodl.LocalizableMessage) [],
   object = 'vim.HostSystem:host-66',
   privilegeId = 'System.View'
}
```

Not sure how to reproduce this as I've no idea what permissions are missed. But script shouldn't fail due to lack of permissions.

vmware_inventory pyVmomi.VmomiSupport.NoPermission
<!---
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_inventory](/ansible/ansible/blob/devel/contrib/inventory/vmware_inventory.py)

##### SUMMARY
<!--- Explain the problem briefly -->

```./vmware_inventory.py``` fails:

```
Traceback (most recent call last):
  File "./vmware_inventory.py", line 721, in <module>
    print(VMWareInventory().show())
  File "./vmware_inventory.py", line 134, in __init__
    self.do_api_calls_update_cache()
  File "./vmware_inventory.py", line 176, in do_api_calls_update_cache
    self.inventory = self.instances_to_inventory(self.get_instances())
  File "./vmware_inventory.py", line 329, in get_instances
    return self._get_instances(kwargs)
  File "./vmware_inventory.py", line 369, in _get_instances
    ifacts = self.facts_from_vobj(instance)
  File "./vmware_inventory.py", line 614, in facts_from_vobj
    inkey=method,
  File "./vmware_inventory.py", line 692, in _process_object_types
    level=(level + 1)
  File "./vmware_inventory.py", line 631, in _process_object_types
    rdata[key] = getattr(vobj, key)
  File "/home/user/vmware/lib/python2.7/site-packages/pyVmomi/VmomiSupport.py", line 574, in __call__
    return self.f(*args, **kwargs)
  File "/home/user/vmware/lib/python2.7/site-packages/pyVmomi/VmomiSupport.py", line 394, in _InvokeAccessor
    return self._stub.InvokeAccessor(self, info)
  File "/home/user/vmware/lib/python2.7/site-packages/pyVmomi/StubAdapterAccessorImpl.py", line 47, in InvokeAccessor
    raise objectContent.missingSet[0].fault
pyVmomi.VmomiSupport.NoPermission: (vim.fault.NoPermission) {
   dynamicType = <unset>,
   dynamicProperty = (vmodl.DynamicProperty) [],
   msg = '',
   faultCause = <unset>,
   faultMessage = (vmodl.LocalizableMessage) [],
   object = 'vim.HostSystem:host-66',
   privilegeId = 'System.View'
}
```

Not sure how to reproduce this as I've no idea what permissions are missed. But script shouldn't fail due to lack of permissions.



Discussion:
What ansible version?
Script is from latest devel, I didn't run it with ansible, just manually.

P.S.: this worked for me:

```python
        if vobj is None:
            rdata = None
        elif type(vobj) in self.vimTable:
            rdata = {}
            for key in self.vimTable[type(vobj)]:
                try:
                    rdata[key] = getattr(vobj, key)
                except Exception as e:
                    logging.error(e, exc_info=True)
```
cc @dav1x @nerzhul
@hryamzik seems like a logical patch. Would you be able to submit a pullrequest?
@jctanner sure, going to fire it tomorrow, I just wasn't sure it's fine. This script doesn't provide me with datacenters and VM folders so I've already moved from it.
What ansible version?
Script is from latest devel, I didn't run it with ansible, just manually.

P.S.: this worked for me:

```python
        if vobj is None:
            rdata = None
        elif type(vobj) in self.vimTable:
            rdata = {}
            for key in self.vimTable[type(vobj)]:
                try:
                    rdata[key] = getattr(vobj, key)
                except Exception as e:
                    logging.error(e, exc_info=True)
```
cc @dav1x @nerzhul
@hryamzik seems like a logical patch. Would you be able to submit a pullrequest?
@jctanner sure, going to fire it tomorrow, I just wasn't sure it's fine. This script doesn't provide me with datacenters and VM folders so I've already moved from it.

Resolving patch:
diff --git a/contrib/inventory/vmware_inventory.py b/contrib/inventory/vmware_inventory.py
--- a/contrib/inventory/vmware_inventory.py
+++ b/contrib/inventory/vmware_inventory.py
@@ -628,7 +628,10 @@ def _process_object_types(self, vobj, thisvm=None, inkey=None, level=0):
         elif type(vobj) in self.vimTable:
             rdata = {}
             for key in self.vimTable[type(vobj)]:
-                rdata[key] = getattr(vobj, key)
+                try:
+                    rdata[key] = getattr(vobj, key)
+                except Exception as e:
+                    self.debugl(e)
 
         elif issubclass(type(vobj), str) or isinstance(vobj, str):
             if vobj.isalnum():


Test patch: