Merge pull request #4398 from jladdjr/instance_id_fallback

Instance id fallback
This commit is contained in:
Jim Ladd
2020-06-11 12:19:23 -07:00
committed by GitHub
3 changed files with 17 additions and 9 deletions

View File

@@ -271,7 +271,7 @@ class Command(BaseCommand):
logging.DEBUG, 0])) logging.DEBUG, 0]))
logger.setLevel(log_levels.get(self.verbosity, 0)) logger.setLevel(log_levels.get(self.verbosity, 0))
def _get_instance_id(self, from_dict, default=''): def _get_instance_id(self, variables, default=''):
''' '''
Retrieve the instance ID from the given dict of host variables. Retrieve the instance ID from the given dict of host variables.
@@ -279,15 +279,23 @@ class Command(BaseCommand):
the lookup will traverse into nested dicts, equivalent to: the lookup will traverse into nested dicts, equivalent to:
from_dict.get('foo', {}).get('bar', default) from_dict.get('foo', {}).get('bar', default)
Multiple ID variables may be specified as 'foo.bar,foobar', so that
it will first try to find 'bar' inside of 'foo', and if unable,
will try to find 'foobar' as a fallback
''' '''
instance_id = default instance_id = default
if getattr(self, 'instance_id_var', None): if getattr(self, 'instance_id_var', None):
for key in self.instance_id_var.split('.'): for single_instance_id in self.instance_id_var.split(','):
if not hasattr(from_dict, 'get'): from_dict = variables
instance_id = default for key in single_instance_id.split('.'):
if not hasattr(from_dict, 'get'):
instance_id = default
break
instance_id = from_dict.get(key, default)
from_dict = instance_id
if instance_id:
break break
instance_id = from_dict.get(key, default)
from_dict = instance_id
return smart_text(instance_id) return smart_text(instance_id)
def _get_enabled(self, from_dict, default=None): def _get_enabled(self, from_dict, default=None):
@@ -422,7 +430,7 @@ class Command(BaseCommand):
for mem_host in self.all_group.all_hosts.values(): for mem_host in self.all_group.all_hosts.values():
instance_id = self._get_instance_id(mem_host.variables) instance_id = self._get_instance_id(mem_host.variables)
if not instance_id: if not instance_id:
logger.warning('Host "%s" has no "%s" variable', logger.warning('Host "%s" has no "%s" variable(s)',
mem_host.name, self.instance_id_var) mem_host.name, self.instance_id_var)
continue continue
mem_host.instance_id = instance_id mem_host.instance_id = instance_id

View File

@@ -2554,7 +2554,7 @@ class RunInventoryUpdate(BaseTask):
args.append('--exclude-empty-groups') args.append('--exclude-empty-groups')
if getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper(), False): if getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper(), False):
args.extend(['--instance-id-var', args.extend(['--instance-id-var',
getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper()),]) "'{}'".format(getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper())),])
# Add arguments for the source inventory script # Add arguments for the source inventory script
args.append('--source') args.append('--source')
args.append(self.pseudo_build_inventory(inventory_update, private_data_dir)) args.append(self.pseudo_build_inventory(inventory_update, private_data_dir))

View File

@@ -727,7 +727,7 @@ VMWARE_ENABLED_VAR = 'guest.gueststate'
VMWARE_ENABLED_VALUE = 'running' VMWARE_ENABLED_VALUE = 'running'
# Inventory variable name containing the unique instance ID. # Inventory variable name containing the unique instance ID.
VMWARE_INSTANCE_ID_VAR = 'config.instanceuuid' VMWARE_INSTANCE_ID_VAR = 'config.instanceUuid, config.instanceuuid'
# Filter for allowed group and host names when importing inventory # Filter for allowed group and host names when importing inventory
# from VMware. # from VMware.