Repository: ansible/awx
File: awx/main/tasks/jobs.py (Python)

Code before:

        if inventory_update.host_filter:
            options['host_filter'] = inventory_update.host_filter

        if getattr(settings, '%s_EXCLUDE_EMPTY_GROUPS' % src.upper()):
            options['exclude_empty_groups'] = True
        if getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper(), False):
            options['instance_id_var'] = getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper())

        # Verbosity is applied to saving process, as well as ansible-inventory CLI option
        if inventory_update.verbosity:
            options['verbosity'] = inventory_update.verbosity

        handler = SpecialInventoryHandler(
            self.runner_callback.event_handler,
            self.runner_callback.cancel_callback,
            verbosity=inventory_update.verbosity,
            job_timeout=self.get_instance_timeout(self.instance),
            start_time=inventory_update.started,
            counter=self.runner_callback.event_ct,
            initial_line=self.runner_callback.end_line,
        )
        inv_logger = logging.getLogger('awx.main.commands.inventory_import')
        formatter = inv_logger.handlers[0].formatter
        formatter.job_start = inventory_update.started
        handler.formatter = formatter
        inv_logger.handlers[0] = handler

        from awx.main.management.commands.inventory_import import Command as InventoryImportCommand

        cmd = InventoryImportCommand()
        try:
            # save the inventory data to database.
            # canceling exceptions will be handled in the global post_run_hook
            cmd.perform_update(options, data, inventory_update)
        except PermissionDenied as exc:
            logger.exception('License error saving {} content'.format(inventory_update.log_format))
            raise PostRunError(str(exc), status='error')
        except PostRunError:
            logger.exception('Error saving {} content, rolling back changes'.format(inventory_update.log_format))
            raise
        except Exception:
            logger.exception('Exception saving {} content, rolling back changes.'.format(inventory_update.log_format))
            raise PostRunError('Error occured while saving inventory data, see traceback or server logs', status='error', tb=traceback.format_exc())


@task(queue=get_local_queuename)
class RunAdHocCommand(BaseTask):
    """
    Run an ad hoc command using ansible.
    """

Review comment (suggestion):
This looks like it may need something added back in. Jobs have an `scm_revision` field to track which revision it ran. We may want to add back in a similar field for inventory updates.

Code after:
            options['instance_id_var'] = getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper())

        # Verbosity is applied to saving process, as well as ansible-inventory CLI option
        if inventory_update.verbosity:
            options['verbosity'] = inventory_update.verbosity

        handler = SpecialInventoryHandler(
            self.runner_callback.event_handler,
            self.runner_callback.cancel_callback,
            verbosity=inventory_update.verbosity,
            job_timeout=self.get_instance_timeout(self.instance),
            start_time=inventory_update.started,
            counter=self.runner_callback.event_ct,
            initial_line=self.runner_callback.end_line,
        )
        inv_logger = logging.getLogger('awx.main.commands.inventory_import')
        formatter = inv_logger.handlers[0].formatter
        formatter.job_start = inventory_update.started
        handler.formatter = formatter
        inv_logger.handlers[0] = handler

        from awx.main.management.commands.inventory_import import Command as InventoryImportCommand

        cmd = InventoryImportCommand()
        try:
            # save the inventory data to database.
            # canceling exceptions will be handled in the global post_run_hook
            cmd.perform_update(options, data, inventory_update)
        except PermissionDenied as exc:
            logger.exception('License error saving {} content'.format(inventory_update.log_format))
            raise PostRunError(str(exc), status='error')
        except PostRunError:
            logger.exception('Error saving {} content, rolling back changes'.format(inventory_update.log_format))
            raise
        except Exception:
            logger.exception('Exception saving {} content, rolling back changes.'.format(inventory_update.log_format))
            raise PostRunError('Error occured while saving inventory data, see traceback or server logs', status='error', tb=traceback.format_exc())


@task(queue=get_local_queuename)
class RunAdHocCommand(BaseTask):
    """
    Run an ad hoc command using ansible.
    """

    model = AdHocCommand
    event_model = AdHocCommandEvent
    callback_class = RunnerCallbackForAdHocCommand

    def build_private_data(self, ad_hoc_command, private_data_dir):
        """