Repository: ansible/ansible

Issue:
VMWare inventory fails on UnicodeDecodeError exception
- Bug Report
##### COMPONENT NAME

contrib/inventory/vmware_inventory.py
##### ANSIBLE VERSION

```
ansible 2.1.1.0
  config file =
  configured module search path = Default w/o overrides

```
##### CONFIGURATION

nothing changed
##### OS / ENVIRONMENT

ubuntu 12.04 / ansible installed from pip
##### SUMMARY
##### STEPS TO REPRODUCE

I launched the inventory script just to test the functionnality, I didn't modify the ini file that comes with it except to set the proper server and password.
When passing `--debug` to the inventory script I can see it goes through the `get_fact`, `pre-filtered hosts`, `filter` and `post-filter` steps.

```
ansible/contrib/inventory/vmware_inventory.py
# few minutes here
Traceback (most recent call last):
  File "ansible/contrib/inventory/vmware_inventory.py", line 552, in <module>
    print(VMWareInventory().show())
  File "ansible/contrib/inventory/vmware_inventory.py", line 109, in __init__
    self.do_api_calls_update_cache()
  File "ansible/contrib/inventory/vmware_inventory.py", line 155, in do_api_calls_update_cache
    self.write_to_cache(self.inventory, self.cache_path_cache)
  File "ansible/contrib/inventory/vmware_inventory.py", line 163, in write_to_cache
    f.write(json.dumps(data))
  File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x99 in position 1: invalid start byte
```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

Return an inventory
##### ACTUAL RESULTS

Exception as seen before



Discussion:
printing the object `instances` I can see this kind of values in the output
    `'vmxconfigchecksum': '\x87\x88B\x18\x8c\xed13v\xc1\x11\x01\x07m\x93\xfb)\x914\xe0'`

@bmillemathias does this patch help at all?

```
diff --git a/contrib/inventory/vmware_inventory.py b/contrib/inventory/vmware_inventory.py
index 7e70a28..3e76bc0 100755
--- a/contrib/inventory/vmware_inventory.py
+++ b/contrib/inventory/vmware_inventory.py
@@ -487,7 +487,10 @@ class VMWareInventory(object):
         if vobj is None:
             rdata = None
         elif issubclass(type(vobj), str) or isinstance(vobj, str):
-            rdata = vobj
+            if vobj.isalnum():
+                rdata = vobj
+            else:
+                rdata = vobj.decode('ascii', 'ignore')
         elif issubclass(type(vobj), bool) or isinstance(vobj, bool):
             rdata = vobj
         elif issubclass(type(vobj), int) or isinstance(vobj, int):
```

needs_info

Hi @jctanner 
I confirm the patch makes the inventory script work.

Thanks !


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
@@ -487,7 +487,10 @@ def _process_object_types(self, vobj, level=0):
         if vobj is None:
             rdata = None
         elif issubclass(type(vobj), str) or isinstance(vobj, str):
-            rdata = vobj
+            if vobj.isalnum():
+                rdata = vobj
+            else:
+                rdata = vobj.decode('ascii', 'ignore')
         elif issubclass(type(vobj), bool) or isinstance(vobj, bool):
             rdata = vobj
         elif issubclass(type(vobj), int) or isinstance(vobj, int):


Test patch: