# Buggy code
def exec_module(self, **kwargs):
    """Main module execution method"""

    for key in list(self.module_arg_spec.keys()):
        if hasattr(self, key):
            setattr(self, key, kwargs[key])
        elif kwargs[key] is not None:
            if key == "location":
                self.parameters["location"] = kwargs[key]
            elif key == "collation":
                self.parameters["collation"] = kwargs[key]
            elif key == "create_mode":
                self.parameters["create_mode"] = _snake_to_camel(kwargs[key], True)
            elif key == "source_database_id":
                self.parameters["source_database_id"] = kwargs[key]
            elif key == "source_database_deletion_date":
                self.parameters["source_database_deletion_date"] = kwargs[key]
            elif key == "restore_point_in_time":
                self.parameters["restore_point_in_time"] = kwargs[key]
            elif key == "recovery_services_recovery_point_resource_id":
                self.parameters["recovery_services_recovery_point_resource_id"] = (
                    kwargs[key]
                )
            elif key == "edition":
                self.parameters["edition"] = _snake_to_camel(kwargs[key], True)
            elif key == "max_size_bytes":
                self.parameters["max_size_bytes"] = kwargs[key]
            elif key == "elastic_pool_name":
                self.parameters["elastic_pool_name"] = kwargs[key]
            elif key == "read_scale":
                self.parameters["read_scale"] = "Enabled" if kwargs[key] else "Disabled"
            elif key == "sample_name":
                ev = kwargs[key]
                if ev == "adventure_works_lt":
                    ev = "AdventureWorksLT"
                self.parameters["sample_name"] = ev
            elif key == "zone_redundant":
                self.parameters["zone_redundant"] = (
                    "Enabled" if kwargs[key] else "Disabled"
                )

    old_response = None
    response = None

    resource_group = self.get_resource_group(self.resource_group)

    if "location" not in self.parameters:
        self.parameters["location"] = resource_group.location

    old_response = self.get_sqldatabase()

    if not old_response:
        self.log("SQL Database instance doesn't exist")
        if self.state == "absent":
            self.log("Old instance didn't exist")
        else:
            self.to_do = Actions.Create
    else:
        self.log("SQL Database instance already exists")
        if self.state == "absent":
            self.to_do = Actions.Delete
        elif self.state == "present":
            self.log(
                "Need to check if SQL Database instance has to be deleted or may be updated"
            )
            if ("location" in self.parameters) and (
                self.parameters["location"] != old_response["location"]
            ):
                self.to_do = Actions.Update
            if ("read_scale" in self.parameters) and (
                self.parameters["read_scale"] != old_response["read_scale"]
            ):
                self.to_do = Actions.Update
            if ("max_size_bytes" in self.parameters) and (
                self.parameters["max_size_bytes"] != old_response["max_size_bytes"]
            ):
                self.to_do = Actions.Update
            if ("edition" in self.parameters) and (
                self.parameters["edition"] != old_response["edition"]
            ):
                self.to_do = Actions.Update

    if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
        self.log("Need to Create / Update the SQL Database instance")

        if self.check_mode:
            self.results["changed"] = True
            return self.results

        response = self.create_update_sqldatabase()

        if not old_response:
            self.results["changed"] = True
        else:
            self.results["changed"] = old_response.__ne__(response)
        self.log("Creation / Update done")
    elif self.to_do == Actions.Delete:
        self.log("SQL Database instance deleted")
        self.results["changed"] = True

        if self.check_mode:
            return self.results

        self.delete_sqldatabase()
        # make sure instance is actually deleted, for some Azure resources, instance is hanging around
        # for some time after deletion -- this should be really fixed in Azure
        while self.get_sqldatabase():
            time.sleep(20)
    else:
        self.log("SQL Database instance unchanged")
        self.results["changed"] = False
        response = old_response

    if response:
        self.results["id"] = response["id"]
        self.results["database_id"] = response["database_id"]
        self.results["status"] = response["status"]

    return self.results


# Traceback (TypeError)
TASK [Creating a SQL database  op18db] **********************************************************************************************************
task path: /mnt/c/source/tmp/scripts/amp:79
Using module file /usr/local/lib/python3.5/dist-packages/ansible/modules/cloud/azure/azure_rm_sqldatabase.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: op
<127.0.0.1> EXEC /bin/sh -c 'echo ~op &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/op/.ansible/tmp/ansible-tmp-1529512576.7917314-267487385500822 `" &amp;&amp; echo ansible-tmp-1529512576.7917314-267487385500822="` echo /home/op/.ansible/tmp/ansible-tmp-1529512576.7917314-267487385500822 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /home/op/.ansible/tmp/ansible-local-6921efz19cjc/tmpwg4r2sow TO /home/op/.ansible/tmp/ansible-tmp-1529512576.7917314-267487385500822/azure_rm_sqldatabase.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/op/.ansible/tmp/ansible-tmp-1529512576.7917314-267487385500822/ /home/op/.ansible/tmp/ansible-tmp-1529512576.7917314-267487385500822/azure_rm_sqldatabase.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /home/op/.ansible/tmp/ansible-tmp-1529512576.7917314-267487385500822/azure_rm_sqldatabase.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/op/.ansible/tmp/ansible-tmp-1529512576.7917314-267487385500822/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1167, in deserialize_data
return self.deserialize_basic(data, data_type)
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1278, in deserialize_basic
raise TypeError("Invalid boolean value: {}".format(attr))
TypeError: Invalid boolean value: Disabled

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 467, in body
data = deserializer(data_type, data)
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1023, in __call__
value = self.deserialize_data(raw_value, attr_desc['type'])
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1185, in deserialize_data
raise_with_traceback(DeserializationError, msg, err)
File "/usr/local/lib/python3.5/dist-packages/msrest/exceptions.py", line 45, in raise_with_traceback
raise error.with_traceback(exc_traceback)
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1167, in deserialize_data
return self.deserialize_basic(data, data_type)
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1278, in deserialize_basic
raise TypeError("Invalid boolean value: {}".format(attr))
msrest.exceptions.DeserializationError: Unable to deserialize response data. Data: Disabled, bool, TypeError: Invalid boolean value: Disabled

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py", line 499, in <module>
main()
File "/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py", line 496, in main
AzureRMDatabases()
File "/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py", line 312, in __init__
supports_tags=False)
File "/tmp/ansible_ow89qgmk/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py", line 345, in __init__
File "/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py", line 395, in exec_module
response = self.create_update_sqldatabase()
File "/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py", line 438, in create_update_sqldatabase
parameters=self.parameters)
File "/usr/local/lib/python3.5/dist-packages/azure/mgmt/sql/operations/databases_operations.py", line 344, in create_or_update
**operation_config
File "/usr/local/lib/python3.5/dist-packages/azure/mgmt/sql/operations/databases_operations.py", line 286, in _create_or_update_initial
body_content = self._serialize.body(parameters, 'Database')
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 470, in body
SerializationError, "Unable to build a model: "+str(err), err)
File "/usr/local/lib/python3.5/dist-packages/msrest/exceptions.py", line 45, in raise_with_traceback
raise error.with_traceback(exc_traceback)
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 467, in body
data = deserializer(data_type, data)
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1023, in __call__
value = self.deserialize_data(raw_value, attr_desc['type'])
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1185, in deserialize_data
raise_with_traceback(DeserializationError, msg, err)
File "/usr/local/lib/python3.5/dist-packages/msrest/exceptions.py", line 45, in raise_with_traceback
raise error.with_traceback(exc_traceback)
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1167, in deserialize_data
return self.deserialize_basic(data, data_type)
File "/usr/local/lib/python3.5/dist-packages/msrest/serialization.py", line 1278, in deserialize_basic
raise TypeError("Invalid boolean value: {}".format(attr))
msrest.exceptions.SerializationError: Unable to build a model: Unable to deserialize response data. Data: Disabled, bool, TypeError: Invalid boolean value: Disabled, DeserializationError: Unable to deserialize response data. Data: Disabled, bool, TypeError: Invalid boolean value: Disabled

fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1167, in deserialize_data\n    return self.deserialize_basic(data, data_type)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1278, in deserialize_basic\n    raise TypeError(\"Invalid boolean value: {}\".format(attr))\nTypeError: Invalid boolean value: Disabled\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 467, in body\n    data = deserializer(data_type, data)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1023, in __call__\n    value = self.deserialize_data(raw_value, attr_desc['type'])\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1185, in deserialize_data\n    raise_with_traceback(DeserializationError, msg, err)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/exceptions.py\", line 45, in raise_with_traceback\n    raise error.with_traceback(exc_traceback)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1167, in deserialize_data\n    return self.deserialize_basic(data, data_type)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1278, in deserialize_basic\n    raise TypeError(\"Invalid boolean value: {}\".format(attr))\nmsrest.exceptions.DeserializationError: Unable to deserialize response data. Data: Disabled, bool, TypeError: Invalid boolean value: Disabled\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py\", line 499, in <module>\n    main()\n  File \"/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py\", line 496, in main\n    AzureRMDatabases()\n  File \"/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py\", line 312, in __init__\n    supports_tags=False)\n  File \"/tmp/ansible_ow89qgmk/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py\", line 345, in __init__\n  File \"/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py\", line 395, in exec_module\n    response = self.create_update_sqldatabase()\n  File \"/tmp/ansible_ow89qgmk/ansible_module_azure_rm_sqldatabase.py\", line 438, in create_update_sqldatabase\n    parameters=self.parameters)\n  File \"/usr/local/lib/python3.5/dist-packages/azure/mgmt/sql/operations/databases_operations.py\", line 344, in create_or_update\n    **operation_config\n  File \"/usr/local/lib/python3.5/dist-packages/azure/mgmt/sql/operations/databases_operations.py\", line 286, in _create_or_update_initial\n    body_content = self._serialize.body(parameters, 'Database')\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 470, in body\n    SerializationError, \"Unable to build a model: \"+str(err), err)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/exceptions.py\", line 45, in raise_with_traceback\n    raise error.with_traceback(exc_traceback)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 467, in body\n    data = deserializer(data_type, data)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1023, in __call__\n    value = self.deserialize_data(raw_value, attr_desc['type'])\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1185, in deserialize_data\n    raise_with_traceback(DeserializationError, msg, err)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/exceptions.py\", line 45, in raise_with_traceback\n    raise error.with_traceback(exc_traceback)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1167, in deserialize_data\n    return self.deserialize_basic(data, data_type)\n  File \"/usr/local/lib/python3.5/dist-packages/msrest/serialization.py\", line 1278, in deserialize_basic\n    raise TypeError(\"Invalid boolean value: {}\".format(attr))\nmsrest.exceptions.SerializationError: Unable to build a model: Unable to deserialize response data. Data: Disabled, bool, TypeError: Invalid boolean value: Disabled, DeserializationError: Unable to deserialize response data. Data: Disabled, bool, TypeError: Invalid boolean value: Disabled\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
to retry, use: --limit @/mnt/c/source/tmp/scripts/amp.retry

PLAY RECAP *******************************************************************************************************************************************
localhost                  : ok=12   changed=2    unreachable=0    failed=1

# Fixed code
def exec_module(self, **kwargs):
    """Main module execution method"""

    for key in list(self.module_arg_spec.keys()):
        if hasattr(self, key):
            setattr(self, key, kwargs[key])
        elif kwargs[key] is not None:
            if key == "location":
                self.parameters["location"] = kwargs[key]
            elif key == "collation":
                self.parameters["collation"] = kwargs[key]
            elif key == "create_mode":
                self.parameters["create_mode"] = _snake_to_camel(kwargs[key], True)
            elif key == "source_database_id":
                self.parameters["source_database_id"] = kwargs[key]
            elif key == "source_database_deletion_date":
                self.parameters["source_database_deletion_date"] = kwargs[key]
            elif key == "restore_point_in_time":
                self.parameters["restore_point_in_time"] = kwargs[key]
            elif key == "recovery_services_recovery_point_resource_id":
                self.parameters["recovery_services_recovery_point_resource_id"] = (
                    kwargs[key]
                )
            elif key == "edition":
                self.parameters["edition"] = _snake_to_camel(kwargs[key], True)
            elif key == "max_size_bytes":
                self.parameters["max_size_bytes"] = kwargs[key]
            elif key == "elastic_pool_name":
                self.parameters["elastic_pool_name"] = kwargs[key]
            elif key == "read_scale":
                self.parameters["read_scale"] = "Enabled" if kwargs[key] else "Disabled"
            elif key == "sample_name":
                ev = kwargs[key]
                if ev == "adventure_works_lt":
                    ev = "AdventureWorksLT"
                self.parameters["sample_name"] = ev
            elif key == "zone_redundant":
                self.parameters["zone_redundant"] = True if kwargs[key] else False

    old_response = None
    response = None

    resource_group = self.get_resource_group(self.resource_group)

    if "location" not in self.parameters:
        self.parameters["location"] = resource_group.location

    old_response = self.get_sqldatabase()

    if not old_response:
        self.log("SQL Database instance doesn't exist")
        if self.state == "absent":
            self.log("Old instance didn't exist")
        else:
            self.to_do = Actions.Create
    else:
        self.log("SQL Database instance already exists")
        if self.state == "absent":
            self.to_do = Actions.Delete
        elif self.state == "present":
            self.log(
                "Need to check if SQL Database instance has to be deleted or may be updated"
            )
            if ("location" in self.parameters) and (
                self.parameters["location"] != old_response["location"]
            ):
                self.to_do = Actions.Update
            if ("read_scale" in self.parameters) and (
                self.parameters["read_scale"] != old_response["read_scale"]
            ):
                self.to_do = Actions.Update
            if ("max_size_bytes" in self.parameters) and (
                self.parameters["max_size_bytes"] != old_response["max_size_bytes"]
            ):
                self.to_do = Actions.Update
            if ("edition" in self.parameters) and (
                self.parameters["edition"] != old_response["edition"]
            ):
                self.to_do = Actions.Update

    if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
        self.log("Need to Create / Update the SQL Database instance")

        if self.check_mode:
            self.results["changed"] = True
            return self.results

        response = self.create_update_sqldatabase()

        if not old_response:
            self.results["changed"] = True
        else:
            self.results["changed"] = old_response.__ne__(response)
        self.log("Creation / Update done")
    elif self.to_do == Actions.Delete:
        self.log("SQL Database instance deleted")
        self.results["changed"] = True

        if self.check_mode:
            return self.results

        self.delete_sqldatabase()
        # make sure instance is actually deleted, for some Azure resources, instance is hanging around
        # for some time after deletion -- this should be really fixed in Azure
        while self.get_sqldatabase():
            time.sleep(20)
    else:
        self.log("SQL Database instance unchanged")
        self.results["changed"] = False
        response = old_response

    if response:
        self.results["id"] = response["id"]
        self.results["database_id"] = response["database_id"]
        self.results["status"] = response["status"]

    return self.results