From beb7e1023bc2e935e16481ca1de2338ee1e666eb Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 15 Mar 2016 14:31:57 -0400 Subject: Group selector feature added --- inventory/multi_inventory.py | 75 +++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 22 deletions(-) (limited to 'inventory') diff --git a/inventory/multi_inventory.py b/inventory/multi_inventory.py index 20fc48aa9..57be259f7 100755 --- a/inventory/multi_inventory.py +++ b/inventory/multi_inventory.py @@ -25,6 +25,9 @@ class MultiInventoryException(Exception): '''Exceptions for MultiInventory class''' pass +# pylint: disable=too-many-public-methods +# After a refactor of too-many-branches and placing those branches into +# their own corresponding function, we have passed the allowed amount of functions(20). class MultiInventory(object): ''' MultiInventory class: @@ -282,35 +285,63 @@ class MultiInventory(object): else: return data.get(keys, None) - def apply_account_config(self, acc_config): - ''' Apply account config settings - ''' - results = self.all_inventory_results[acc_config['name']] - results['all_hosts'] = results['_meta']['hostvars'].keys() - + def apply_extra_vars(self, inventory, extra_vars): + ''' Apply the account config extra vars ''' # Extra vars go here - for new_var, value in acc_config.get('extra_vars', {}).items(): - for data in results['_meta']['hostvars'].values(): + for new_var, value in extra_vars.items(): + for data in inventory.values(): self.add_entry(data, new_var, value) + def apply_clone_vars(self, inventory, clone_vars): + ''' Apply the account config clone vars ''' # Clone vars go here - for to_name, from_name in acc_config.get('clone_vars', {}).items(): - for data in results['_meta']['hostvars'].values(): + for to_name, from_name in clone_vars.items(): + for data in inventory.values(): self.add_entry(data, to_name, self.get_entry(data, from_name)) - # Extra groups go here - for new_var, value in acc_config.get('extra_groups', {}).items(): - for data in results['_meta']['hostvars'].values(): - results["%s_%s" % (new_var, value)] = copy.copy(results['all_hosts']) - - # Clone groups go here - # Build a group based on the desired key name - for to_name, from_name in acc_config.get('clone_groups', {}).items(): - for name, data in results['_meta']['hostvars'].items(): + def apply_extra_groups(self, inventory, extra_groups): + ''' Apply the account config for extra groups ''' + _ = self # Here for pylint. wanted an instance method instead of static + for new_var, value in extra_groups.items(): + for _ in inventory['_meta']['hostvars'].values(): + inventory["%s_%s" % (new_var, value)] = copy.copy(inventory['all_hosts']) + + def apply_clone_groups(self, inventory, clone_groups): + ''' Apply the account config for clone groups ''' + for to_name, from_name in clone_groups.items(): + for name, data in inventory['_meta']['hostvars'].items(): key = '%s_%s' % (to_name, self.get_entry(data, from_name)) - if not results.has_key(key): - results[key] = [] - results[key].append(name) + if not inventory.has_key(key): + inventory[key] = [] + inventory[key].append(name) + + def apply_group_selectors(self, inventory, group_selectors): + ''' Apply the account config for clone groups ''' + _ = self # Here for pylint. wanted an instance method instead of static + for selector in group_selectors: + if inventory.has_key(selector['from_group']): + inventory[selector['from_group']].sort() + inventory[selector['name']] = inventory[selector['from_group']][0:selector['count']] + for host in inventory[selector['from_group']]: + if host in inventory[selector['name']]: + inventory['_meta']['hostvars'][host][selector['name']] = True + else: + inventory['_meta']['hostvars'][host][selector['name']] = False + + def apply_account_config(self, acc_config): + ''' Apply account config settings ''' + results = self.all_inventory_results[acc_config['name']] + results['all_hosts'] = results['_meta']['hostvars'].keys() + + self.apply_extra_vars(results['_meta']['hostvars'], acc_config.get('extra_vars', {})) + + self.apply_clone_vars(results['_meta']['hostvars'], acc_config.get('clone_vars', {})) + + self.apply_extra_groups(results, acc_config.get('extra_groups', {})) + + self.apply_clone_groups(results, acc_config.get('clone_groups', {})) + + self.apply_group_selectors(results, acc_config.get('group_selectors', {})) # store the results back into all_inventory_results self.all_inventory_results[acc_config['name']] = results -- cgit v1.2.3