# Buggy code
def _delete_hosts(self):
    """
    For each host in the database that is NOT in the local list, delete
    it. When importing from a cloud inventory source attached to a
    specific group, only delete hosts beneath that group.  Delete each
    host individually so signal handlers will run.
    """
    if settings.SQL_DEBUG:
        queries_before = len(connection.queries)
    hosts_qs = self.inventory_source.hosts
    # Build list of all host pks, remove all that should not be deleted.
    del_host_pks = set(hosts_qs.values_list("pk", flat=True))
    if self.instance_id_var:
        all_instance_ids = self.mem_instance_id_map.keys()
        instance_ids = []
        for offset in range(0, len(all_instance_ids), self._batch_size):
            instance_ids = all_instance_ids[offset : (offset + self._batch_size)]
            for host_pk in hosts_qs.filter(instance_id__in=instance_ids).values_list(
                "pk", flat=True
            ):
                del_host_pks.discard(host_pk)
        for host_pk in set(
            [v for k, v in self.db_instance_id_map.items() if k in instance_ids]
        ):
            del_host_pks.discard(host_pk)
        all_host_names = list(
            set(self.mem_instance_id_map.values())
            - set(self.all_group.all_hosts.keys())
        )
    else:
        all_host_names = list(self.all_group.all_hosts.keys())
    for offset in range(0, len(all_host_names), self._batch_size):
        host_names = all_host_names[offset : (offset + self._batch_size)]
        for host_pk in hosts_qs.filter(name__in=host_names).values_list(
            "pk", flat=True
        ):
            del_host_pks.discard(host_pk)
    # Now delete all remaining hosts in batches.
    all_del_pks = sorted(list(del_host_pks))
    for offset in range(0, len(all_del_pks), self._batch_size):
        del_pks = all_del_pks[offset : (offset + self._batch_size)]
        for host in hosts_qs.filter(pk__in=del_pks):
            host_name = host.name
            host.delete()
            logger.debug('Deleted host "%s"', host_name)
    if settings.SQL_DEBUG:
        logger.warning(
            "host deletions took %d queries for %d hosts",
            len(connection.queries) - queries_before,
            len(all_del_pks),
        )


# Traceback (RuntimeError)
1.570 INFO     Updating inventory 3: roman cluster
1.584 INFO     Reading Ansible inventory source: /var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/plugins/inventory/ec2.py
1.586 INFO     Using VIRTUAL_ENV: /var/lib/awx/venv/my-custom-venv/
1.586 INFO     Using PATH: /var/lib/awx/venv/my-custom-venv/bin:/var/lib/awx/venv/awx/bin:/var/lib/awx/venv/awx/bin:/var/lib/awx/venv/awx/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
1.586 INFO     Using PYTHONPATH: /var/lib/awx/venv/my-custom-venv/lib/python3.6/site-packages:
Traceback (most recent call last):
File "/var/lib/awx/venv/awx/bin/awx-manage", line 11, in <module>
load_entry_point('awx==3.0.0.0', 'console_scripts', 'awx-manage')()
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/__init__.py", line 150, in manage
execute_from_command_line(sys.argv)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/main/management/commands/inventory_import.py", line 1030, in handle
raise exc
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/main/management/commands/inventory_import.py", line 930, in handle
data = AnsibleInventoryLoader(source=source, is_custom=self.is_custom, venv_path=venv_path).load()
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/main/management/commands/inventory_import.py", line 176, in load
return self.command_to_json(base_args + ['--list'])
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/main/management/commands/inventory_import.py", line 159, in command_to_json
self.method, proc.returncode, stdout, stderr))
RuntimeError: ansible-inventory failed (rc=1) with stdout:
stderr:
Traceback (most recent call last):
File "/usr/bin/ansible-inventory", line 67, in <module>
import ansible.constants as C
File "/var/lib/awx/venv/my-custom-venv/lib/python3.6/site-packages/ansible/constants.py", line 17, in <module>
from ansible.config.manager import ConfigManager, ensure_type, get_ini_config_value
File "/var/lib/awx/venv/my-custom-venv/lib/python3.6/site-packages/ansible/config/manager.py", line 16, in <module>
from yaml import load as yaml_load
File "/var/lib/awx/venv/my-custom-venv/lib/python3.6/site-packages/yaml/__init__.py", line 284
class YAMLObject(metaclass=YAMLObjectMetaclass):
^
SyntaxError: invalid syntax

# Fixed code
def _delete_hosts(self):
    """
    For each host in the database that is NOT in the local list, delete
    it. When importing from a cloud inventory source attached to a
    specific group, only delete hosts beneath that group.  Delete each
    host individually so signal handlers will run.
    """
    if settings.SQL_DEBUG:
        queries_before = len(connection.queries)
    hosts_qs = self.inventory_source.hosts
    # Build list of all host pks, remove all that should not be deleted.
    del_host_pks = set(hosts_qs.values_list("pk", flat=True))
    if self.instance_id_var:
        all_instance_ids = list(self.mem_instance_id_map.keys())
        instance_ids = []
        for offset in range(0, len(all_instance_ids), self._batch_size):
            instance_ids = all_instance_ids[offset : (offset + self._batch_size)]
            for host_pk in hosts_qs.filter(instance_id__in=instance_ids).values_list(
                "pk", flat=True
            ):
                del_host_pks.discard(host_pk)
        for host_pk in set(
            [v for k, v in self.db_instance_id_map.items() if k in instance_ids]
        ):
            del_host_pks.discard(host_pk)
        all_host_names = list(
            set(self.mem_instance_id_map.values())
            - set(self.all_group.all_hosts.keys())
        )
    else:
        all_host_names = list(self.all_group.all_hosts.keys())
    for offset in range(0, len(all_host_names), self._batch_size):
        host_names = all_host_names[offset : (offset + self._batch_size)]
        for host_pk in hosts_qs.filter(name__in=host_names).values_list(
            "pk", flat=True
        ):
            del_host_pks.discard(host_pk)
    # Now delete all remaining hosts in batches.
    all_del_pks = sorted(list(del_host_pks))
    for offset in range(0, len(all_del_pks), self._batch_size):
        del_pks = all_del_pks[offset : (offset + self._batch_size)]
        for host in hosts_qs.filter(pk__in=del_pks):
            host_name = host.name
            host.delete()
            logger.debug('Deleted host "%s"', host_name)
    if settings.SQL_DEBUG:
        logger.warning(
            "host deletions took %d queries for %d hosts",
            len(connection.queries) - queries_before,
            len(all_del_pks),
        )