# Buggy code
def main():
    argument_spec = openstack_full_argument_spec(
        name=dict(required=True),
        password=dict(required=False, default=None, no_log=True),
        email=dict(required=False, default=None),
        default_project=dict(required=False, default=None),
        description=dict(type="str"),
        domain=dict(required=False, default=None),
        enabled=dict(default=True, type="bool"),
        state=dict(default="present", choices=["absent", "present"]),
        update_password=dict(default=None, choices=["always", "on_create"]),
    )

    module_kwargs = openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    name = module.params["name"]
    password = module.params.get("password")
    email = module.params["email"]
    default_project = module.params["default_project"]
    domain = module.params["domain"]
    enabled = module.params["enabled"]
    state = module.params["state"]
    update_password = module.params["update_password"]
    description = module.params["description"]

    sdk, cloud = openstack_cloud_from_module(module)
    try:
        user = cloud.get_user(name)

        domain_id = None
        if domain:
            domain_id = _get_domain_id(cloud, domain)

        if state == "present":
            if update_password in ("always", "on_create"):
                if not password:
                    msg = (
                        "update_password is %s but a password value is missing"
                        % update_password
                    )
                    module.fail_json(msg=msg)
            default_project_id = None
            if default_project:
                default_project_id = _get_default_project_id(
                    cloud, default_project, module
                )

            if user is None:
                if description is not None:
                    user = cloud.create_user(
                        name=name,
                        password=password,
                        email=email,
                        default_project=default_project_id,
                        domain_id=domain_id,
                        enabled=enabled,
                        description=description,
                    )
                else:
                    user = cloud.create_user(
                        name=name,
                        password=password,
                        email=email,
                        default_project=default_project_id,
                        domain_id=domain_id,
                        enabled=enabled,
                    )
                changed = True
            else:
                params_dict = {
                    "email": email,
                    "enabled": enabled,
                    "password": password,
                    "update_password": update_password,
                }
                if description is not None:
                    params_dict["description"] = description
                if domain_id is not None:
                    params_dict["domain_id"] = domain_id
                if default_project_id is not None:
                    params_dict["default_project_id"] = default_project_id

                if _needs_update(params_dict, user):
                    if update_password == "always":
                        if description is not None:
                            user = cloud.update_user(
                                user["id"],
                                password=password,
                                email=email,
                                default_project=default_project_id,
                                domain_id=domain_id,
                                enabled=enabled,
                                description=description,
                            )
                        else:
                            user = cloud.update_user(
                                user["id"],
                                password=password,
                                email=email,
                                default_project=default_project_id,
                                domain_id=domain_id,
                                enabled=enabled,
                            )
                    else:
                        if description is not None:
                            user = cloud.update_user(
                                user["id"],
                                email=email,
                                default_project=default_project_id,
                                domain_id=domain_id,
                                enabled=enabled,
                                description=description,
                            )
                        else:
                            user = cloud.update_user(
                                user["id"],
                                email=email,
                                default_project=default_project_id,
                                domain_id=domain_id,
                                enabled=enabled,
                            )
                    changed = True
                else:
                    changed = False
            module.exit_json(changed=changed, user=user)

        elif state == "absent":
            if user is None:
                changed = False
            else:
                cloud.delete_user(user["id"])
                changed = True
            module.exit_json(changed=changed)

    except sdk.exceptions.OpenStackCloudException as e:
        module.fail_json(msg=str(e), extra_data=e.extra_data)


# Traceback (nTypeError)
"module_stderr": "OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016\r\ndebug1: Reading configuration data /home/ckoester/.ssh/config\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 19: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 16121\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\nShared connection to inception-ngena3.ngena.ciscolabs.com closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_oGfmKg/ansible_module_os_project.py\", line 209, in <module>\r\n    main()\r\n  File \"/tmp/ansible_oGfmKg/ansible_module_os_project.py\", line 154, in main\r\n    sdk, cloud = openstack_cloud_from_module(module)\r\n  File \"/tmp/ansible_oGfmKg/ansible_modlib.zip/ansible/module_utils/openstack.py\", line 139, in openstack_cloud_from_module\r\nTypeError: fail_json() takes exactly 1 argument (2 given)\r\n",

# Fixed code
def main():
    argument_spec = openstack_full_argument_spec(
        name=dict(required=True),
        password=dict(required=False, default=None, no_log=True),
        email=dict(required=False, default=None),
        default_project=dict(required=False, default=None),
        description=dict(type="str"),
        domain=dict(required=False, default=None),
        enabled=dict(default=True, type="bool"),
        state=dict(default="present", choices=["absent", "present"]),
        update_password=dict(default=None, choices=["always", "on_create"]),
    )

    module_kwargs = openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    name = module.params["name"]
    password = module.params.get("password")
    email = module.params["email"]
    default_project = module.params["default_project"]
    domain = module.params["domain"]
    enabled = module.params["enabled"]
    state = module.params["state"]
    update_password = module.params["update_password"]
    description = module.params["description"]

    sdk, cloud = openstack_cloud_from_module(module)
    try:
        domain_id = None
        if domain:
            domain_id = _get_domain_id(cloud, domain)
            user = cloud.get_user(name, domain_id=domain_id)
        else:
            user = cloud.get_user(name)

        if state == "present":
            if update_password in ("always", "on_create"):
                if not password:
                    msg = (
                        "update_password is %s but a password value is missing"
                        % update_password
                    )
                    module.fail_json(msg=msg)
            default_project_id = None
            if default_project:
                default_project_id = _get_default_project_id(
                    cloud, default_project, module
                )

            if user is None:
                if description is not None:
                    user = cloud.create_user(
                        name=name,
                        password=password,
                        email=email,
                        default_project=default_project_id,
                        domain_id=domain_id,
                        enabled=enabled,
                        description=description,
                    )
                else:
                    user = cloud.create_user(
                        name=name,
                        password=password,
                        email=email,
                        default_project=default_project_id,
                        domain_id=domain_id,
                        enabled=enabled,
                    )
                changed = True
            else:
                params_dict = {
                    "email": email,
                    "enabled": enabled,
                    "password": password,
                    "update_password": update_password,
                }
                if description is not None:
                    params_dict["description"] = description
                if domain_id is not None:
                    params_dict["domain_id"] = domain_id
                if default_project_id is not None:
                    params_dict["default_project_id"] = default_project_id

                if _needs_update(params_dict, user):
                    if update_password == "always":
                        if description is not None:
                            user = cloud.update_user(
                                user["id"],
                                password=password,
                                email=email,
                                default_project=default_project_id,
                                domain_id=domain_id,
                                enabled=enabled,
                                description=description,
                            )
                        else:
                            user = cloud.update_user(
                                user["id"],
                                password=password,
                                email=email,
                                default_project=default_project_id,
                                domain_id=domain_id,
                                enabled=enabled,
                            )
                    else:
                        if description is not None:
                            user = cloud.update_user(
                                user["id"],
                                email=email,
                                default_project=default_project_id,
                                domain_id=domain_id,
                                enabled=enabled,
                                description=description,
                            )
                        else:
                            user = cloud.update_user(
                                user["id"],
                                email=email,
                                default_project=default_project_id,
                                domain_id=domain_id,
                                enabled=enabled,
                            )
                    changed = True
                else:
                    changed = False
            module.exit_json(changed=changed, user=user)

        elif state == "absent":
            if user is None:
                changed = False
            else:
                if domain:
                    cloud.delete_user(user["id"], domain_id=domain_id)
                else:
                    cloud.delete_user(user["id"])
                changed = True
            module.exit_json(changed=changed)

    except sdk.exceptions.OpenStackCloudException as e:
        module.fail_json(msg=str(e), extra_data=e.extra_data)