# Buggy code
def main():
    argument_spec = dict(
        name=dict(),
        description=dict(),
        organization=dict(),
        scm_type=dict(choices=["manual", "git", "hg", "svn"], default="manual"),
        scm_url=dict(),
        scm_branch=dict(),
        scm_credential=dict(),
        scm_clean=dict(type="bool", default=False),
        scm_delete_on_update=dict(type="bool", default=False),
        scm_update_on_launch=dict(type="bool", default=False),
        scm_update_cache_timeout=dict(type="int", default=0),
        job_timeout=dict(type="int", default=0),
        custom_virtualenv=dict(),
        local_path=dict(),
        state=dict(choices=["present", "absent"], default="present"),
    )

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

    name = module.params.get("name")
    description = module.params.get("description")
    organization = module.params.get("organization")
    scm_type = module.params.get("scm_type")
    if scm_type == "manual":
        scm_type = ""
    scm_url = module.params.get("scm_url")
    local_path = module.params.get("local_path")
    scm_branch = module.params.get("scm_branch")
    scm_credential = module.params.get("scm_credential")
    scm_clean = module.params.get("scm_clean")
    scm_delete_on_update = module.params.get("scm_delete_on_update")
    scm_update_on_launch = module.params.get("scm_update_on_launch")
    scm_update_cache_timeout = module.params.get("scm_update_cache_timeout")
    job_timeout = module.params.get("job_timeout")
    custom_virtualenv = module.params.get("custom_virtualenv")
    state = module.params.get("state")

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

    tower_auth = tower_auth_config(module)
    with settings.runtime_values(**tower_auth):
        tower_check_mode(module)
        project = tower_cli.get_resource("project")
        try:
            if state == "present":
                try:
                    org_res = tower_cli.get_resource("organization")
                    org = org_res.get(name=organization)
                except exc.NotFound as excinfo:
                    module.fail_json(
                        msg="Failed to update project, organization not found: {0}".format(
                            organization
                        ),
                        changed=False,
                    )

                if scm_credential:
                    try:
                        cred_res = tower_cli.get_resource("credential")
                        try:
                            cred = cred_res.get(name=scm_credential)
                        except (
                            tower_cli.exceptions.MultipleResults
                        ) as multi_res_excinfo:
                            module.warn(
                                "Multiple credentials found for {0}, falling back looking in project organization".format(
                                    scm_credential
                                )
                            )
                            cred = cred_res.get(
                                name=scm_credential, organization=org["id"]
                            )
                        scm_credential = cred["id"]
                    except exc.NotFound as excinfo:
                        module.fail_json(
                            msg="Failed to update project, credential not found: {0}".format(
                                scm_credential
                            ),
                            changed=False,
                        )

                if (scm_update_cache_timeout is not None) and (
                    scm_update_on_launch is not True
                ):
                    module.warn(
                        "scm_update_cache_timeout will be ignored since scm_update_on_launch was not set to true"
                    )

                result = project.modify(
                    name=name,
                    description=description,
                    organization=org["id"],
                    scm_type=scm_type,
                    scm_url=scm_url,
                    local_path=local_path,
                    scm_branch=scm_branch,
                    scm_clean=scm_clean,
                    credential=scm_credential,
                    scm_delete_on_update=scm_delete_on_update,
                    scm_update_on_launch=scm_update_on_launch,
                    scm_update_cache_timeout=scm_update_cache_timeout,
                    job_timeout=job_timeout,
                    custom_virtualenv=custom_virtualenv,
                    create_on_missing=True,
                )
                json_output["id"] = result["id"]
            elif state == "absent":
                result = project.delete(name=name)
        except (exc.ConnectionError, exc.BadRequest) as excinfo:
            module.fail_json(
                msg="Failed to update project: {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(),
        description=dict(),
        organization=dict(),
        scm_type=dict(choices=["manual", "git", "hg", "svn"], default="manual"),
        scm_url=dict(),
        scm_branch=dict(),
        scm_credential=dict(),
        scm_clean=dict(type="bool", default=False),
        scm_delete_on_update=dict(type="bool", default=False),
        scm_update_on_launch=dict(type="bool", default=False),
        scm_update_cache_timeout=dict(type="int", default=0),
        job_timeout=dict(type="int", default=0),
        custom_virtualenv=dict(),
        local_path=dict(),
        state=dict(choices=["present", "absent"], default="present"),
    )

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

    name = module.params.get("name")
    description = module.params.get("description")
    organization = module.params.get("organization")
    scm_type = module.params.get("scm_type")
    if scm_type == "manual":
        scm_type = ""
    scm_url = module.params.get("scm_url")
    local_path = module.params.get("local_path")
    scm_branch = module.params.get("scm_branch")
    scm_credential = module.params.get("scm_credential")
    scm_clean = module.params.get("scm_clean")
    scm_delete_on_update = module.params.get("scm_delete_on_update")
    scm_update_on_launch = module.params.get("scm_update_on_launch")
    scm_update_cache_timeout = module.params.get("scm_update_cache_timeout")
    job_timeout = module.params.get("job_timeout")
    custom_virtualenv = module.params.get("custom_virtualenv")
    state = module.params.get("state")

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

    tower_auth = tower_auth_config(module)
    with settings.runtime_values(**tower_auth):
        tower_check_mode(module)
        project = tower_cli.get_resource("project")
        try:
            if state == "present":
                try:
                    org_res = tower_cli.get_resource("organization")
                    org = org_res.get(name=organization)
                except exc.NotFound as excinfo:
                    module.fail_json(
                        msg="Failed to update project, organization not found: {0}".format(
                            organization
                        ),
                        changed=False,
                    )

                if scm_credential:
                    try:
                        cred_res = tower_cli.get_resource("credential")
                        try:
                            cred = cred_res.get(name=scm_credential)
                        except (
                            tower_cli.exceptions.MultipleResults
                        ) as multi_res_excinfo:
                            module.warn(
                                "Multiple credentials found for {0}, falling back looking in project organization".format(
                                    scm_credential
                                )
                            )
                            cred = cred_res.get(
                                name=scm_credential, organization=org["id"]
                            )
                        scm_credential = cred["id"]
                    except exc.NotFound as excinfo:
                        module.fail_json(
                            msg="Failed to update project, credential not found: {0}".format(
                                scm_credential
                            ),
                            changed=False,
                        )

                if (scm_update_cache_timeout is not None) and (
                    scm_update_on_launch is not True
                ):
                    module.warn(
                        "scm_update_cache_timeout will be ignored since scm_update_on_launch was not set to true"
                    )

                result = project.modify(
                    name=name,
                    description=description,
                    organization=org["id"],
                    scm_type=scm_type,
                    scm_url=scm_url,
                    local_path=local_path,
                    scm_branch=scm_branch,
                    scm_clean=scm_clean,
                    credential=scm_credential,
                    scm_delete_on_update=scm_delete_on_update,
                    scm_update_on_launch=scm_update_on_launch,
                    scm_update_cache_timeout=scm_update_cache_timeout,
                    job_timeout=job_timeout,
                    custom_virtualenv=custom_virtualenv,
                    create_on_missing=True,
                )
                json_output["id"] = result["id"]
            elif state == "absent":
                result = project.delete(name=name)
        except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo:
            module.fail_json(
                msg="Failed to update project: {0}".format(excinfo), changed=False
            )

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