# Buggy code
def execute_import(self):
    """used to import a role into Ansible Galaxy"""

    colors = {
        "INFO": "normal",
        "WARNING": C.COLOR_WARN,
        "ERROR": C.COLOR_ERROR,
        "SUCCESS": C.COLOR_OK,
        "FAILED": C.COLOR_ERROR,
    }

    if len(context.CLIARGS["args"]) < 2:
        raise AnsibleError(
            "Expected a github_username and github_repository. Use --help."
        )

    github_user = to_text(context.CLIARGS["args"][0], errors="surrogate_or_strict")
    github_repo = to_text(context.CLIARGS["args"][1], errors="surrogate_or_strict")

    if context.CLIARGS["check_status"]:
        task = self.api.get_import_task(
            github_user=github_user, github_repo=github_repo
        )
    else:
        # Submit an import request
        task = self.api.create_import_task(
            github_user,
            github_repo,
            reference=context.CLIARGS["reference"],
            role_name=context.CLIARGS["role_name"],
        )

        if len(task) > 1:
            # found multiple roles associated with github_user/github_repo
            display.display(
                "WARNING: More than one Galaxy role associated with Github repo %s/%s."
                % (github_user, github_repo),
                color="yellow",
            )
            display.display(
                "The following Galaxy roles are being updated:" + "\n",
                color=C.COLOR_CHANGED,
            )
            for t in task:
                display.display(
                    "%s.%s"
                    % (
                        t["summary_fields"]["role"]["namespace"],
                        t["summary_fields"]["role"]["name"],
                    ),
                    color=C.COLOR_CHANGED,
                )
            display.display(
                "\nTo properly namespace this role, remove each of the above and re-import %s/%s from scratch"
                % (github_user, github_repo),
                color=C.COLOR_CHANGED,
            )
            return 0
        # found a single role as expected
        display.display("Successfully submitted import request %d" % task[0]["id"])
        if not context.CLIARGS["wait"]:
            display.display("Role name: %s" % task[0]["summary_fields"]["role"]["name"])
            display.display(
                "Repo: %s/%s" % (task[0]["github_user"], task[0]["github_repo"])
            )

    if context.CLIARGS["check_status"] or context.CLIARGS["wait"]:
        # Get the status of the import
        msg_list = []
        finished = False
        while not finished:
            task = self.api.get_import_task(task_id=task[0]["id"])
            for msg in task[0]["summary_fields"]["task_messages"]:
                if msg["id"] not in msg_list:
                    display.display(
                        msg["message_text"], color=colors[msg["message_type"]]
                    )
                    msg_list.append(msg["id"])
            if task[0]["state"] in ["SUCCESS", "FAILED"]:
                finished = True
            else:
                time.sleep(10)

    return 0


# Traceback (KeyError)
ansible-galaxy 2.9.0.dev0
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/calvin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/calvin/projects/ansible/lib/ansible
executable location = /home/calvin/.local/share/virtualenvs/orion/bin/ansible-galaxy
python version = 3.6.8 (default, Mar 21 2019, 10:08:12) [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)]
Using /etc/ansible/ansible.cfg as config file
Opened /home/calvin/.ansible_galaxy
ERROR! Unexpected Exception, this is probably a bug: 'args'
the full traceback was:

Traceback (most recent call last):
File "/home/calvin/projects/ansible/bin/ansible-galaxy", line 111, in <module>
exit_code = cli.run()
File "/home/calvin/projects/ansible/lib/ansible/cli/galaxy.py", line 268, in run
context.CLIARGS['func']()
File "/home/calvin/projects/ansible/lib/ansible/cli/galaxy.py", line 832, in execute_import
if len(context.CLIARGS['args']) < 2:
File "/home/calvin/projects/ansible/lib/ansible/module_utils/common/collections.py", line 20, in __getitem__
return self._store[key]
KeyError: 'args'

# Fixed code
def execute_import(self):
    """used to import a role into Ansible Galaxy"""

    colors = {
        "INFO": "normal",
        "WARNING": C.COLOR_WARN,
        "ERROR": C.COLOR_ERROR,
        "SUCCESS": C.COLOR_OK,
        "FAILED": C.COLOR_ERROR,
    }

    github_user = to_text(context.CLIARGS["github_user"], errors="surrogate_or_strict")
    github_repo = to_text(context.CLIARGS["github_repo"], errors="surrogate_or_strict")

    if context.CLIARGS["check_status"]:
        task = self.api.get_import_task(
            github_user=github_user, github_repo=github_repo
        )
    else:
        # Submit an import request
        task = self.api.create_import_task(
            github_user,
            github_repo,
            reference=context.CLIARGS["reference"],
            role_name=context.CLIARGS["role_name"],
        )

        if len(task) > 1:
            # found multiple roles associated with github_user/github_repo
            display.display(
                "WARNING: More than one Galaxy role associated with Github repo %s/%s."
                % (github_user, github_repo),
                color="yellow",
            )
            display.display(
                "The following Galaxy roles are being updated:" + "\n",
                color=C.COLOR_CHANGED,
            )
            for t in task:
                display.display(
                    "%s.%s"
                    % (
                        t["summary_fields"]["role"]["namespace"],
                        t["summary_fields"]["role"]["name"],
                    ),
                    color=C.COLOR_CHANGED,
                )
            display.display(
                "\nTo properly namespace this role, remove each of the above and re-import %s/%s from scratch"
                % (github_user, github_repo),
                color=C.COLOR_CHANGED,
            )
            return 0
        # found a single role as expected
        display.display("Successfully submitted import request %d" % task[0]["id"])
        if not context.CLIARGS["wait"]:
            display.display("Role name: %s" % task[0]["summary_fields"]["role"]["name"])
            display.display(
                "Repo: %s/%s" % (task[0]["github_user"], task[0]["github_repo"])
            )

    if context.CLIARGS["check_status"] or context.CLIARGS["wait"]:
        # Get the status of the import
        msg_list = []
        finished = False
        while not finished:
            task = self.api.get_import_task(task_id=task[0]["id"])
            for msg in task[0]["summary_fields"]["task_messages"]:
                if msg["id"] not in msg_list:
                    display.display(
                        msg["message_text"], color=colors[msg["message_type"]]
                    )
                    msg_list.append(msg["id"])
            if task[0]["state"] in ["SUCCESS", "FAILED"]:
                finished = True
            else:
                time.sleep(10)

    return 0