# Buggy code
def install(self, path, b_temp_path):
    if self.skip:
        display.display("Skipping '%s' as it is already installed" % to_text(self))
        return

    # Install if it is not
    collection_path = os.path.join(path, self.namespace, self.name)
    b_collection_path = to_bytes(collection_path, errors="surrogate_or_strict")
    display.display(
        "Installing '%s:%s' to '%s'"
        % (to_text(self), self.latest_version, collection_path)
    )

    if self.b_path is None:
        download_url = self._metadata.download_url
        artifact_hash = self._metadata.artifact_sha256
        headers = {}
        self.api._add_auth_token(headers, download_url, required=False)

        self.b_path = _download_file(
            download_url,
            b_temp_path,
            artifact_hash,
            self.api.validate_certs,
            headers=headers,
        )

    if os.path.exists(b_collection_path):
        shutil.rmtree(b_collection_path)
    os.makedirs(b_collection_path)

    try:
        with tarfile.open(self.b_path, mode="r") as collection_tar:
            files_member_obj = collection_tar.getmember("FILES.json")
            with _tarfile_extract(collection_tar, files_member_obj) as files_obj:
                files = json.loads(
                    to_text(files_obj.read(), errors="surrogate_or_strict")
                )

            _extract_tar_file(
                collection_tar, "MANIFEST.json", b_collection_path, b_temp_path
            )
            _extract_tar_file(
                collection_tar, "FILES.json", b_collection_path, b_temp_path
            )

            for file_info in files["files"]:
                file_name = file_info["name"]
                if file_name == ".":
                    continue

                if file_info["ftype"] == "file":
                    _extract_tar_file(
                        collection_tar,
                        file_name,
                        b_collection_path,
                        b_temp_path,
                        expected_hash=file_info["chksum_sha256"],
                    )
                else:
                    os.makedirs(
                        os.path.join(
                            b_collection_path,
                            to_bytes(file_name, errors="surrogate_or_strict"),
                        )
                    )
    except Exception:
        # Ensure we don't leave the dir behind in case of a failure.
        shutil.rmtree(b_collection_path)

        b_namespace_path = os.path.dirname(b_collection_path)
        if not os.listdir(b_namespace_path):
            os.rmdir(b_namespace_path)

        raise


# Traceback (KeyError)
Exception, this is probably a bug: "filename 'tests/unit/modules/placebo_recordings/aws_direct_connect_link_aggregation_group/nonexistent_lag_status' not found"
the full traceback was:
Traceback (most recent call last):
File "/home/bcoca/work/ansible/bin/ansible-galaxy", line 123, in <module>
exit_code = cli.run()
File "/home/bcoca/work/ansible/lib/ansible/cli/galaxy.py", line 490, in run
context.CLIARGS['func']()
File "/home/bcoca/work/ansible/lib/ansible/cli/galaxy.py", line 1057, in execute_install
self._execute_install_collection(collection_requirements, collection_path)
File "/home/bcoca/work/ansible/lib/ansible/cli/galaxy.py", line 1079, in _execute_install_collection
no_deps, force, force_with_deps, allow_pre_release=allow_pre_release)
File "/home/bcoca/work/ansible/lib/ansible/galaxy/collection.py", line 675, in install_collections
collection.install(output_path, b_temp_path)
File "/home/bcoca/work/ansible/lib/ansible/galaxy/collection.py", line 250, in install
self.install_artifact(b_collection_path, b_temp_path)
File "/home/bcoca/work/ansible/lib/ansible/galaxy/collection.py", line 275, in install_artifact
_extract_tar_dir(collection_tar, file_name, b_collection_path)
File "/home/bcoca/work/ansible/lib/ansible/galaxy/collection.py", line 1387, in _extract_tar_dir
tar_member = tar.getmember(to_native(dirname, errors='surrogate_or_strict'))
File "/usr/lib/python3.7/tarfile.py", line 1752, in getmember
raise KeyError("filename %r not found" % name)
KeyError: "filename 'tests/unit/modules/placebo_recordings/aws_direct_connect_link_aggregation_group/nonexistent_lag_status' not found"

# Fixed code
def install(self, path, b_temp_path):
    if self.skip:
        display.display("Skipping '%s' as it is already installed" % to_text(self))
        return

    # Install if it is not
    collection_path = os.path.join(path, self.namespace, self.name)
    b_collection_path = to_bytes(collection_path, errors="surrogate_or_strict")
    display.display(
        "Installing '%s:%s' to '%s'"
        % (to_text(self), self.latest_version, collection_path)
    )

    if self.b_path is None:
        download_url = self._metadata.download_url
        artifact_hash = self._metadata.artifact_sha256
        headers = {}
        self.api._add_auth_token(headers, download_url, required=False)

        self.b_path = _download_file(
            download_url,
            b_temp_path,
            artifact_hash,
            self.api.validate_certs,
            headers=headers,
        )

    if os.path.exists(b_collection_path):
        shutil.rmtree(b_collection_path)
    os.makedirs(b_collection_path)

    try:
        with tarfile.open(self.b_path, mode="r") as collection_tar:
            files_member_obj = collection_tar.getmember("FILES.json")
            with _tarfile_extract(collection_tar, files_member_obj) as (
                dummy,
                files_obj,
            ):
                files = json.loads(
                    to_text(files_obj.read(), errors="surrogate_or_strict")
                )

            _extract_tar_file(
                collection_tar, "MANIFEST.json", b_collection_path, b_temp_path
            )
            _extract_tar_file(
                collection_tar, "FILES.json", b_collection_path, b_temp_path
            )

            for file_info in files["files"]:
                file_name = file_info["name"]
                if file_name == ".":
                    continue

                if file_info["ftype"] == "file":
                    _extract_tar_file(
                        collection_tar,
                        file_name,
                        b_collection_path,
                        b_temp_path,
                        expected_hash=file_info["chksum_sha256"],
                    )

                else:
                    _extract_tar_dir(collection_tar, file_name, b_collection_path)

    except Exception:
        # Ensure we don't leave the dir behind in case of a failure.
        shutil.rmtree(b_collection_path)

        b_namespace_path = os.path.dirname(b_collection_path)
        if not os.listdir(b_namespace_path):
            os.rmdir(b_namespace_path)

        raise