diff --git a/awx/main/management/commands/inventory_import.py b/awx/main/management/commands/inventory_import.py index 14ce8d389b..2e51283445 100644 --- a/awx/main/management/commands/inventory_import.py +++ b/awx/main/management/commands/inventory_import.py @@ -271,7 +271,7 @@ class Command(BaseCommand): logging.DEBUG, 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. @@ -279,15 +279,23 @@ class Command(BaseCommand): the lookup will traverse into nested dicts, equivalent to: 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 if getattr(self, 'instance_id_var', None): - for key in self.instance_id_var.split('.'): - if not hasattr(from_dict, 'get'): - instance_id = default + for single_instance_id in self.instance_id_var.split(','): + from_dict = variables + 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 - instance_id = from_dict.get(key, default) - from_dict = instance_id return smart_text(instance_id) 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(): instance_id = self._get_instance_id(mem_host.variables) 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) continue mem_host.instance_id = instance_id diff --git a/awx/main/tasks.py b/awx/main/tasks.py index ef2de1a500..8fc3de1b6b 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -2552,7 +2552,7 @@ class RunInventoryUpdate(BaseTask): args.append('--exclude-empty-groups') if getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper(), False): 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 args.append('--source') args.append(self.pseudo_build_inventory(inventory_update, private_data_dir)) diff --git a/awx/settings/defaults.py b/awx/settings/defaults.py index 8df6d4f440..f5507262bf 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -721,7 +721,7 @@ VMWARE_ENABLED_VAR = 'guest.gueststate' VMWARE_ENABLED_VALUE = 'running' # 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 # from VMware.