From 21877b33783667db4dd545a02852a5748d4b31dc Mon Sep 17 00:00:00 2001 From: Adam Edwards Date: Tue, 6 Nov 2018 14:59:26 -0800 Subject: [PATCH] update to latest vmware_inventory.py with support for groupby_custom_field_excludes https://github.com/ansible/ansible/blob/e364d717cb952217df92585b82bdd2b04af7f649/contrib/inventory/vmware_inventory.py Signed-off-by: Adam Edwards --- awx/plugins/inventory/vmware_inventory.py | 35 +++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/awx/plugins/inventory/vmware_inventory.py b/awx/plugins/inventory/vmware_inventory.py index 28977a3aed..310194e164 100755 --- a/awx/plugins/inventory/vmware_inventory.py +++ b/awx/plugins/inventory/vmware_inventory.py @@ -9,7 +9,7 @@ # TODO: # * more jq examples -# * optional folder heriarchy +# * optional folder hierarchy """ $ jq '._meta.hostvars[].config' data.json | head @@ -38,9 +38,8 @@ import sys import uuid from time import time -import six from jinja2 import Environment -from six import integer_types, string_types +from six import integer_types, PY3 from six.moves import configparser try: @@ -99,6 +98,7 @@ class VMWareInventory(object): host_filters = [] skip_keys = [] groupby_patterns = [] + groupby_custom_field_excludes = [] safe_types = [bool, str, float, None] + list(integer_types) iter_types = [dict, list] @@ -230,10 +230,11 @@ class VMWareInventory(object): 'groupby_patterns': '{{ guest.guestid }},{{ "templates" if config.template else "guests"}}', 'lower_var_keys': True, 'custom_field_group_prefix': 'vmware_tag_', + 'groupby_custom_field_excludes': '', 'groupby_custom_field': False} } - if six.PY3: + if PY3: config = configparser.ConfigParser() else: config = configparser.SafeConfigParser() @@ -304,8 +305,12 @@ class VMWareInventory(object): groupby_pattern += "}}" self.groupby_patterns.append(groupby_pattern) self.debugl('groupby patterns are %s' % self.groupby_patterns) + temp_groupby_custom_field_excludes = config.get('vmware', 'groupby_custom_field_excludes') + self.groupby_custom_field_excludes = [x.strip('"') for x in [y.strip("'") for y in temp_groupby_custom_field_excludes.split(",")]] + self.debugl('groupby exclude strings are %s' % self.groupby_custom_field_excludes) + # Special feature to disable the brute force serialization of the - # virtulmachine objects. The key name for these properties does not + # virtual machine objects. The key name for these properties does not # matter because the values are just items for a larger list. if config.has_section('properties'): self.guest_props = [] @@ -397,7 +402,7 @@ class VMWareInventory(object): cfm = content.customFieldsManager if cfm is not None and cfm.field: for f in cfm.field: - if f.managedObjectType == vim.VirtualMachine: + if not f.managedObjectType or f.managedObjectType == vim.VirtualMachine: self.custom_fields[f.key] = f.name self.debugl('%d custom fields collected' % len(self.custom_fields)) except vmodl.RuntimeFault as exc: @@ -494,16 +499,15 @@ class VMWareInventory(object): for k, v in inventory['_meta']['hostvars'].items(): if 'customvalue' in v: for tv in v['customvalue']: - if not isinstance(tv['value'], string_types): - continue - newkey = None field_name = self.custom_fields[tv['key']] if tv['key'] in self.custom_fields else tv['key'] + if field_name in self.groupby_custom_field_excludes: + continue values = [] keylist = map(lambda x: x.strip(), tv['value'].split(',')) for kl in keylist: try: - newkey = self.config.get('vmware', 'custom_field_group_prefix') + str(field_name) + '_' + kl + newkey = "%s%s_%s" % (self.config.get('vmware', 'custom_field_group_prefix'), str(field_name), kl) newkey = newkey.strip() except Exception as e: self.debugl(e) @@ -521,7 +525,6 @@ class VMWareInventory(object): def create_template_mapping(self, inventory, pattern, dtype='string'): ''' Return a hash of uuid to templated string from pattern ''' - mapping = {} for k, v in inventory['_meta']['hostvars'].items(): t = self.env.from_string(pattern) @@ -557,7 +560,15 @@ class VMWareInventory(object): if '.' not in prop: # props without periods are direct attributes of the parent - rdata[key] = getattr(vm, prop) + vm_property = getattr(vm, prop) + if isinstance(vm_property, vim.CustomFieldsManager.Value.Array): + temp_vm_property = [] + for vm_prop in vm_property: + temp_vm_property.append({'key': vm_prop.key, + 'value': vm_prop.value}) + rdata[key] = temp_vm_property + else: + rdata[key] = vm_property else: # props with periods are subkeys of parent attributes parts = prop.split('.')