From 7a7e1941b1e7315b77fd5b1b2d5e39ccec1f5c06 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Mon, 12 Oct 2015 11:44:58 -0400 Subject: [PATCH] Update vmware module from ansible --- awx/plugins/inventory/vmware.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/awx/plugins/inventory/vmware.py b/awx/plugins/inventory/vmware.py index 27330b8bcd..8f723a638d 100755 --- a/awx/plugins/inventory/vmware.py +++ b/awx/plugins/inventory/vmware.py @@ -28,6 +28,8 @@ take precedence over options present in the INI file. An INI file is not required if these options are specified using environment variables. ''' +from __future__ import print_function + import collections import json import logging @@ -37,6 +39,8 @@ import sys import time import ConfigParser +from six import text_type + # Disable logging message trigged by pSphere/suds. try: from logging import NullHandler @@ -95,7 +99,7 @@ class VMwareInventory(object): Saves the value to cache with the name given. ''' if self.config.has_option('defaults', 'cache_dir'): - cache_dir = self.config.get('defaults', 'cache_dir') + cache_dir = os.path.expanduser(self.config.get('defaults', 'cache_dir')) if not os.path.exists(cache_dir): os.makedirs(cache_dir) cache_file = os.path.join(cache_dir, name) @@ -115,7 +119,7 @@ class VMwareInventory(object): else: cache_max_age = 0 cache_stat = os.stat(cache_file) - if (cache_stat.st_mtime + cache_max_age) < time.time(): + if (cache_stat.st_mtime + cache_max_age) >= time.time(): with open(cache_file) as cache: return json.load(cache) return default @@ -147,7 +151,7 @@ class VMwareInventory(object): seen = seen or set() if isinstance(obj, ManagedObject): try: - obj_unicode = unicode(getattr(obj, 'name')) + obj_unicode = text_type(getattr(obj, 'name')) except AttributeError: obj_unicode = () if obj in seen: @@ -164,7 +168,7 @@ class VMwareInventory(object): obj_info = self._get_obj_info(val, depth - 1, seen) if obj_info != (): d[attr] = obj_info - except Exception, e: + except Exception as e: pass return d elif isinstance(obj, SudsObject): @@ -207,8 +211,8 @@ class VMwareInventory(object): host_info[k] = v try: host_info['ipAddress'] = host.config.network.vnic[0].spec.ip.ipAddress - except Exception, e: - print >> sys.stderr, e + except Exception as e: + print(e, file=sys.stderr) host_info = self._flatten_dict(host_info, prefix) if ('%s_ipAddress' % prefix) in host_info: host_info['ansible_ssh_host'] = host_info['%s_ipAddress' % prefix]