# Buggy code
def main():
    argument_spec = dict(
        name=dict(required=True),
        user=dict(),
        team=dict(),
        kind=dict(required=True, choices=KIND_CHOICES.keys()),
        host=dict(),
        username=dict(),
        password=dict(no_log=True),
        ssh_key_data=dict(no_log=True, type="str"),
        ssh_key_unlock=dict(no_log=True),
        authorize=dict(type="bool", default=False),
        authorize_password=dict(no_log=True),
        client=dict(),
        security_token=dict(),
        secret=dict(),
        tenant=dict(),
        subscription=dict(),
        domain=dict(),
        become_method=dict(),
        become_username=dict(),
        become_password=dict(no_log=True),
        vault_password=dict(no_log=True),
        description=dict(),
        organization=dict(required=True),
        project=dict(),
        state=dict(choices=["present", "absent"], default="present"),
    )

    module = TowerModule(argument_spec=argument_spec, supports_check_mode=True)

    name = module.params.get("name")
    organization = module.params.get("organization")
    state = module.params.get("state")

    json_output = {"credential": name, "state": state}

    tower_auth = tower_auth_config(module)
    with settings.runtime_values(**tower_auth):
        tower_check_mode(module)
        credential = tower_cli.get_resource("credential")
        try:
            params = {}
            params["create_on_missing"] = True
            params["name"] = name

            if organization:
                org_res = tower_cli.get_resource("organization")
                org = org_res.get(name=organization)
                params["organization"] = org["id"]

            try:
                tower_cli.get_resource("credential_type")
            except (ImportError, AttributeError):
                # /api/v1/ backwards compat
                # older versions of tower-cli don't *have* a credential_type
                # resource
                params["kind"] = module.params["kind"]
            else:
                credential_type = credential_type_for_v1_kind(module.params, module)
                params["credential_type"] = credential_type["id"]

            if module.params.get("description"):
                params["description"] = module.params.get("description")

            if module.params.get("user"):
                user_res = tower_cli.get_resource("user")
                user = user_res.get(username=module.params.get("user"))
                params["user"] = user["id"]

            if module.params.get("team"):
                team_res = tower_cli.get_resource("team")
                team = team_res.get(name=module.params.get("team"))
                params["team"] = team["id"]

            if module.params.get("ssh_key_data"):
                data = module.params.get("ssh_key_data")
                if os.path.exists(data):
                    module.deprecate(
                        msg="ssh_key_data should be a string, not a path to a file. Use lookup('file', '/path/to/file') instead",
                        version="2.12",
                    )
                    if os.path.isdir(data):
                        module.fail_json(
                            msg="attempted to read contents of directory: %s" % data
                        )
                    with open(data, "rb") as f:
                        module.params["ssh_key_data"] = to_text(f.read())
                else:
                    module.params["ssh_key_data"] = data

            for key in (
                "authorize",
                "authorize_password",
                "client",
                "security_token",
                "secret",
                "tenant",
                "subscription",
                "domain",
                "become_method",
                "become_username",
                "become_password",
                "vault_password",
                "project",
                "host",
                "username",
                "password",
                "ssh_key_data",
                "ssh_key_unlock",
            ):
                if "kind" in params:
                    params[key] = module.params.get(key)
                elif module.params.get(key):
                    params.setdefault("inputs", {})[key] = module.params.get(key)

            if state == "present":
                result = credential.modify(**params)
                json_output["id"] = result["id"]
            elif state == "absent":
                result = credential.delete(**params)
        except exc.NotFound as excinfo:
            module.fail_json(
                msg="Failed to update credential, organization not found: {0}".format(
                    excinfo
                ),
                changed=False,
            )
        except (exc.ConnectionError, exc.BadRequest, exc.NotFound) as excinfo:
            module.fail_json(
                msg="Failed to update credential: {0}".format(excinfo), changed=False
            )

    json_output["changed"] = result["changed"]
    module.exit_json(**json_output)


# Traceback (tower_cli.exceptions.AuthError)
TASK [Set AWX admin account] *******************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: tower_cli.exceptions.AuthError: Invalid Tower authentication credentials (HTTP 401).
fatal: [awx1]: FAILED! => changed=false
module_stderr: |-
Traceback (most recent call last):
File "/home/d069683/.ansible/tmp/ansible-tmp-1546595798.81-18639309082017/AnsiballZ_tower_user.py", line 113, in <module>
_ansiballz_main()
File "/home/d069683/.ansible/tmp/ansible-tmp-1546595798.81-18639309082017/AnsiballZ_tower_user.py", line 105, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/d069683/.ansible/tmp/ansible-tmp-1546595798.81-18639309082017/AnsiballZ_tower_user.py", line 48, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/tmp/ansible_tower_user_payload_uMnpMh/__main__.py", line 131, in <module>
File "/tmp/ansible_tower_user_payload_uMnpMh/__main__.py", line 118, in main
File "/usr/local/lib/python2.7/dist-packages/tower_cli/models/base.py", line 698, in modify
return self.write(pk, create_on_missing=create_on_missing, force_on_exists=True, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tower_cli/models/base.py", line 361, in write
**kwargs
File "/usr/local/lib/python2.7/dist-packages/tower_cli/models/base.py", line 223, in _lookup
existing_data = self.get(include_debug_header=include_debug_header, **read_params)
File "/usr/local/lib/python2.7/dist-packages/tower_cli/models/base.py", line 486, in get
response = self.read(pk=pk, fail_on_no_results=True, fail_on_multiple_results=True, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tower_cli/models/base.py", line 288, in read
r = client.get(url, params=kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 521, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tower_cli/api.py", line 255, in request
raise exc.AuthError('Invalid Tower authentication credentials (HTTP 401).')
tower_cli.exceptions.AuthError: Invalid Tower authentication credentials (HTTP 401).
module_stdout: ''
msg: |-
MODULE FAILURE
See stdout/stderr for the exact error
rc: 1

# Fixed code
def main():
    argument_spec = dict(
        name=dict(required=True),
        user=dict(),
        team=dict(),
        kind=dict(required=True, choices=KIND_CHOICES.keys()),
        host=dict(),
        username=dict(),
        password=dict(no_log=True),
        ssh_key_data=dict(no_log=True, type="str"),
        ssh_key_unlock=dict(no_log=True),
        authorize=dict(type="bool", default=False),
        authorize_password=dict(no_log=True),
        client=dict(),
        security_token=dict(),
        secret=dict(),
        tenant=dict(),
        subscription=dict(),
        domain=dict(),
        become_method=dict(),
        become_username=dict(),
        become_password=dict(no_log=True),
        vault_password=dict(no_log=True),
        description=dict(),
        organization=dict(required=True),
        project=dict(),
        state=dict(choices=["present", "absent"], default="present"),
    )

    module = TowerModule(argument_spec=argument_spec, supports_check_mode=True)

    name = module.params.get("name")
    organization = module.params.get("organization")
    state = module.params.get("state")

    json_output = {"credential": name, "state": state}

    tower_auth = tower_auth_config(module)
    with settings.runtime_values(**tower_auth):
        tower_check_mode(module)
        credential = tower_cli.get_resource("credential")
        try:
            params = {}
            params["create_on_missing"] = True
            params["name"] = name

            if organization:
                org_res = tower_cli.get_resource("organization")
                org = org_res.get(name=organization)
                params["organization"] = org["id"]

            try:
                tower_cli.get_resource("credential_type")
            except (ImportError, AttributeError):
                # /api/v1/ backwards compat
                # older versions of tower-cli don't *have* a credential_type
                # resource
                params["kind"] = module.params["kind"]
            else:
                credential_type = credential_type_for_v1_kind(module.params, module)
                params["credential_type"] = credential_type["id"]

            if module.params.get("description"):
                params["description"] = module.params.get("description")

            if module.params.get("user"):
                user_res = tower_cli.get_resource("user")
                user = user_res.get(username=module.params.get("user"))
                params["user"] = user["id"]

            if module.params.get("team"):
                team_res = tower_cli.get_resource("team")
                team = team_res.get(name=module.params.get("team"))
                params["team"] = team["id"]

            if module.params.get("ssh_key_data"):
                data = module.params.get("ssh_key_data")
                if os.path.exists(data):
                    module.deprecate(
                        msg="ssh_key_data should be a string, not a path to a file. Use lookup('file', '/path/to/file') instead",
                        version="2.12",
                    )
                    if os.path.isdir(data):
                        module.fail_json(
                            msg="attempted to read contents of directory: %s" % data
                        )
                    with open(data, "rb") as f:
                        module.params["ssh_key_data"] = to_text(f.read())
                else:
                    module.params["ssh_key_data"] = data

            for key in (
                "authorize",
                "authorize_password",
                "client",
                "security_token",
                "secret",
                "tenant",
                "subscription",
                "domain",
                "become_method",
                "become_username",
                "become_password",
                "vault_password",
                "project",
                "host",
                "username",
                "password",
                "ssh_key_data",
                "ssh_key_unlock",
            ):
                if "kind" in params:
                    params[key] = module.params.get(key)
                elif module.params.get(key):
                    params.setdefault("inputs", {})[key] = module.params.get(key)

            if state == "present":
                result = credential.modify(**params)
                json_output["id"] = result["id"]
            elif state == "absent":
                result = credential.delete(**params)
        except exc.NotFound as excinfo:
            module.fail_json(
                msg="Failed to update credential, organization not found: {0}".format(
                    excinfo
                ),
                changed=False,
            )
        except (
            exc.ConnectionError,
            exc.BadRequest,
            exc.NotFound,
            exc.AuthError,
        ) as excinfo:
            module.fail_json(
                msg="Failed to update credential: {0}".format(excinfo), changed=False
            )

    json_output["changed"] = result["changed"]
    module.exit_json(**json_output)