From 1a0330488f49a4ba36eeb41c8f1f91d228045caf Mon Sep 17 00:00:00 2001 From: tod-uma Date: Wed, 18 Jul 2018 12:09:16 -0400 Subject: [PATCH] VMware: blacklist custom fields in vmware_inventory.py (#36877) * Provide the ability to blacklist custom fields from having groups created for them. * Updated configuration parameter name --- contrib/inventory/vmware_inventory.ini | 7 +++++++ contrib/inventory/vmware_inventory.py | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/contrib/inventory/vmware_inventory.ini b/contrib/inventory/vmware_inventory.ini index 859f42ca30..bdffd4c2b9 100644 --- a/contrib/inventory/vmware_inventory.ini +++ b/contrib/inventory/vmware_inventory.ini @@ -93,6 +93,13 @@ password=vmware # vmware_tag_ prefix is the default and consistent with ec2_tag_ #custom_field_group_prefix = vmware_tag_ +# You can blacklist custom fields so that they are not included in the +# groupby_custom_field option. This is useful when you have custom fields that +# have values that are unique to individual hosts. Timestamps for example. +# The groupby_custom_field_excludes option should be a comma separated list of custom +# field keys to be blacklisted. +#groupby_custom_field_excludes=,, + # The script attempts to recurse into virtualmachine objects and serialize # all available data. The serialization is comprehensive but slow. If the # vcenter environment is large and the desired properties are known, create diff --git a/contrib/inventory/vmware_inventory.py b/contrib/inventory/vmware_inventory.py index 855c46778d..844abab4d9 100755 --- a/contrib/inventory/vmware_inventory.py +++ b/contrib/inventory/vmware_inventory.py @@ -99,6 +99,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,6 +231,7 @@ 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} } @@ -304,8 +306,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 = [] @@ -496,6 +502,8 @@ class VMWareInventory(object): for tv in v['customvalue']: 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: