# Buggy code
def __init__(self, args):
    self._args = args
    self._cloud_environment = None
    self._compute_client = None
    self._resource_client = None
    self._network_client = None

    self.debug = False
    if args.debug:
        self.debug = True

    self.credentials = self._get_credentials(args)
    if not self.credentials:
        self.fail(
            "Failed to get credentials. Either pass as parameters, set environment variables, "
            "or define a profile in ~/.azure/credentials."
        )

    # if cloud_environment specified, look up/build Cloud object
    raw_cloud_env = self.credentials.get("cloud_environment")
    if not raw_cloud_env:
        self._cloud_environment = azure_cloud.AZURE_PUBLIC_CLOUD  # SDK default
    else:
        # try to look up "well-known" values via the name attribute on azure_cloud members
        all_clouds = [
            x[1]
            for x in inspect.getmembers(azure_cloud)
            if isinstance(x[1], azure_cloud.Cloud)
        ]
        matched_clouds = [x for x in all_clouds if x.name == raw_cloud_env]
        if len(matched_clouds) == 1:
            self._cloud_environment = matched_clouds[0]
        elif len(matched_clouds) > 1:
            self.fail(
                "Azure SDK failure: more than one cloud matched for cloud_environment name '{0}'".format(
                    raw_cloud_env
                )
            )
        else:
            if not urlparse.urlparse(raw_cloud_env).scheme:
                self.fail(
                    "cloud_environment must be an endpoint discovery URL or one of {0}".format(
                        [x.name for x in all_clouds]
                    )
                )
            try:
                self._cloud_environment = azure_cloud.get_cloud_from_metadata_endpoint(
                    raw_cloud_env
                )
            except Exception as e:
                self.fail(
                    "cloud_environment {0} could not be resolved: {1}".format(
                        raw_cloud_env, e.message
                    )
                )

    if self.credentials.get("subscription_id", None) is None:
        self.fail("Credentials did not include a subscription_id value.")
    self.log("setting subscription_id")
    self.subscription_id = self.credentials["subscription_id"]

    if self.credentials.get("credentials"):
        self.azure_credentials = self.credentials.get("credentials")
    elif (
        self.credentials.get("client_id")
        and self.credentials.get("secret")
        and self.credentials.get("tenant")
    ):
        self.azure_credentials = ServicePrincipalCredentials(
            client_id=self.credentials["client_id"],
            secret=self.credentials["secret"],
            tenant=self.credentials["tenant"],
            cloud_environment=self._cloud_environment,
        )
    elif (
        self.credentials.get("ad_user") is not None
        and self.credentials.get("password") is not None
    ):
        tenant = self.credentials.get("tenant")
        if not tenant:
            tenant = "common"
        self.azure_credentials = UserPassCredentials(
            self.credentials["ad_user"],
            self.credentials["password"],
            tenant=tenant,
            cloud_environment=self._cloud_environment,
        )
    else:
        self.fail(
            "Failed to authenticate with provided credentials. Some attributes were missing. "
            "Credentials must include client_id, secret and tenant or ad_user and password."
        )


# Traceback (msrest.exceptions.AuthenticationError)
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_jGyGkY/ansible_module_azure_rm_publicipaddress.py", line 276, in <module>
main()
File "/tmp/ansible_jGyGkY/ansible_module_azure_rm_publicipaddress.py", line 273, in main
AzureRMPublicIPAddress()
File "/tmp/ansible_jGyGkY/ansible_module_azure_rm_publicipaddress.py", line 169, in __init__
supports_check_mode=True)
File "/tmp/ansible_jGyGkY/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py", line 334, in __init__
File "/usr/local/lib/python2.7/dist-packages/msrestazure/azure_active_directory.py", line 367, in __init__
self.set_token()
File "/usr/local/lib/python2.7/dist-packages/msrestazure/azure_active_directory.py", line 404, in set_token
raise_with_traceback(AuthenticationError, "", err)
File "/usr/local/lib/python2.7/dist-packages/msrest/exceptions.py", line 48, in raise_with_traceback
raise error
msrest.exceptions.AuthenticationError: , InvalidGrantError: (invalid_grant) AADSTS70002: Error validating credentials. AADSTS50126: Invalid username or password
Trace ID: 484222f4-b98f-4bb5-a831-97ed7efb1400
Correlation ID: 1172590a-9cb3-4357-b0d0-512b31128e07
Timestamp: 2018-02-23 09:05:33Z

fatal: [linuxhost1]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_jGyGkY/ansible_module_azure_rm_publicipaddress.py\", line 276, in <module>\n    main()\n  File \"/tmp/ansible_jGyGkY/ansible_module_azure_rm_publicipaddress.py\", line 273, in main\n    AzureRMPublicIPAddress()\n  File \"/tmp/ansible_jGyGkY/ansible_module_azure_rm_publicipaddress.py\", line 169, in __init__\n    supports_check_mode=True)\n  File \"/tmp/ansible_jGyGkY/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py\", line 334, in __init__\n  File \"/usr/local/lib/python2.7/dist-packages/msrestazure/azure_active_directory.py\", line 367, in __init__\n    self.set_token()\n  File \"/usr/local/lib/python2.7/dist-packages/msrestazure/azure_active_directory.py\", line 404, in set_token\n    raise_with_traceback(AuthenticationError, \"\", err)\n  File \"/usr/local/lib/python2.7/dist-packages/msrest/exceptions.py\", line 48, in raise_with_traceback\n    raise error\nmsrest.exceptions.AuthenticationError: , InvalidGrantError: (invalid_grant) AADSTS70002: Error validating credentials. AADSTS50126: Invalid username or password\r\nTrace ID: 484222f4-b98f-4bb5-a831-97ed7efb1400\r\nCorrelation ID: 1172590a-9cb3-4357-b0d0-512b31128e07\r\nTimestamp: 2018-02-23 09:05:33Z\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def __init__(self, args):
    self._args = args
    self._cloud_environment = None
    self._compute_client = None
    self._resource_client = None
    self._network_client = None
    self._adfs_authority_url = None
    self._resource = None

    self.debug = False
    if args.debug:
        self.debug = True

    self.credentials = self._get_credentials(args)
    if not self.credentials:
        self.fail(
            "Failed to get credentials. Either pass as parameters, set environment variables, "
            "or define a profile in ~/.azure/credentials."
        )

    # if cloud_environment specified, look up/build Cloud object
    raw_cloud_env = self.credentials.get("cloud_environment")
    if not raw_cloud_env:
        self._cloud_environment = azure_cloud.AZURE_PUBLIC_CLOUD  # SDK default
    else:
        # try to look up "well-known" values via the name attribute on azure_cloud members
        all_clouds = [
            x[1]
            for x in inspect.getmembers(azure_cloud)
            if isinstance(x[1], azure_cloud.Cloud)
        ]
        matched_clouds = [x for x in all_clouds if x.name == raw_cloud_env]
        if len(matched_clouds) == 1:
            self._cloud_environment = matched_clouds[0]
        elif len(matched_clouds) > 1:
            self.fail(
                "Azure SDK failure: more than one cloud matched for cloud_environment name '{0}'".format(
                    raw_cloud_env
                )
            )
        else:
            if not urlparse.urlparse(raw_cloud_env).scheme:
                self.fail(
                    "cloud_environment must be an endpoint discovery URL or one of {0}".format(
                        [x.name for x in all_clouds]
                    )
                )
            try:
                self._cloud_environment = azure_cloud.get_cloud_from_metadata_endpoint(
                    raw_cloud_env
                )
            except Exception as e:
                self.fail(
                    "cloud_environment {0} could not be resolved: {1}".format(
                        raw_cloud_env, e.message
                    )
                )

    if self.credentials.get("subscription_id", None) is None:
        self.fail("Credentials did not include a subscription_id value.")
    self.log("setting subscription_id")
    self.subscription_id = self.credentials["subscription_id"]

    # get authentication authority
    # for adfs, user could pass in authority or not.
    # for others, use default authority from cloud environment
    if self.credentials.get("adfs_authority_url") is None:
        self._adfs_authority_url = self._cloud_environment.endpoints.active_directory
    else:
        self._adfs_authority_url = self.credentials.get("adfs_authority_url")

    # get resource from cloud environment
    self._resource = self._cloud_environment.endpoints.active_directory_resource_id

    if self.credentials.get("credentials"):
        self.azure_credentials = self.credentials.get("credentials")
    elif (
        self.credentials.get("client_id")
        and self.credentials.get("secret")
        and self.credentials.get("tenant")
    ):
        self.azure_credentials = ServicePrincipalCredentials(
            client_id=self.credentials["client_id"],
            secret=self.credentials["secret"],
            tenant=self.credentials["tenant"],
            cloud_environment=self._cloud_environment,
        )

    elif (
        self.credentials.get("ad_user") is not None
        and self.credentials.get("password") is not None
        and self.credentials.get("client_id") is not None
        and self.credentials.get("tenant") is not None
    ):
        self.azure_credentials = self.acquire_token_with_username_password(
            self._adfs_authority_url,
            self._resource,
            self.credentials["ad_user"],
            self.credentials["password"],
            self.credentials["client_id"],
            self.credentials["tenant"],
        )

    elif (
        self.credentials.get("ad_user") is not None
        and self.credentials.get("password") is not None
    ):
        tenant = self.credentials.get("tenant")
        if not tenant:
            tenant = "common"
        self.azure_credentials = UserPassCredentials(
            self.credentials["ad_user"],
            self.credentials["password"],
            tenant=tenant,
            cloud_environment=self._cloud_environment,
        )

    else:
        self.fail(
            "Failed to authenticate with provided credentials. Some attributes were missing. "
            "Credentials must include client_id, secret and tenant or ad_user and password, or "
            "ad_user, password, client_id, tenant and adfs_authority_url(optional) for ADFS authentication, or "
            "be logged in using AzureCLI."
        )