# Buggy code
def main():
    """
    This function is the main function of this module
    """
    # argument_spec = postgres_common_argument_spec()
    argument_spec = dict()
    argument_spec.update(
        address=dict(type="str", default="samehost", aliases=["source", "src"]),
        backup_file=dict(type="str"),
        contype=dict(type="str", default=None, choices=PG_HBA_TYPES),
        create=dict(type="bool", default=False),
        databases=dict(type="str", default="all"),
        dest=dict(type="path", required=True),
        method=dict(type="str", default="md5", choices=PG_HBA_METHODS),
        netmask=dict(type="str"),
        options=dict(type="str"),
        order=dict(type="str", default="sdu", choices=PG_HBA_ORDERS),
        state=dict(type="str", default="present", choices=["absent", "present"]),
        users=dict(type="str", default="all"),
    )
    module = AnsibleModule(
        argument_spec=argument_spec, add_file_common_args=True, supports_check_mode=True
    )
    if not HAS_IPADDRESS:
        module.fail_json(
            msg=missing_required_lib("psycopg2"), exception=IPADDRESS_IMP_ERR
        )

    contype = module.params["contype"]
    create = bool(module.params["create"] or module.check_mode)
    if module.check_mode:
        backup = False
    else:
        backup = module.params["backup"]
        backup_file = module.params["backup_file"]
    databases = module.params["databases"]
    dest = module.params["dest"]

    method = module.params["method"]
    netmask = module.params["netmask"]
    options = module.params["options"]
    order = module.params["order"]
    source = module.params["address"]
    state = module.params["state"]
    users = module.params["users"]

    ret = {"msgs": []}
    try:
        pg_hba = PgHba(dest, order, backup=backup, create=create)
    except PgHbaError as error:
        module.fail_json(msg="Error reading file:\n{0}".format(error))

    if contype:
        try:
            for database in databases.split(","):
                for user in users.split(","):
                    rule = PgHbaRule(
                        contype, database, user, source, netmask, method, options
                    )
                    if state == "present":
                        ret["msgs"].append("Adding")
                        pg_hba.add_rule(rule)
                    else:
                        ret["msgs"].append("Removing")
                        pg_hba.remove_rule(rule)
        except PgHbaError as error:
            module.fail_json(msg="Error modifying rules:\n{0}".format(error))
        file_args = module.load_file_common_arguments(module.params)
        ret["changed"] = changed = pg_hba.changed()
        if changed:
            ret["msgs"].append("Changed")
            ret["diff"] = pg_hba.diff

            if not module.check_mode:
                ret["msgs"].append("Writing")
                try:
                    if pg_hba.write(backup_file):
                        module.set_fs_attributes_if_different(
                            file_args, True, pg_hba.diff, expand=False
                        )
                except PgHbaError as error:
                    module.fail_json(msg="Error writing file:\n{0}".format(error))
                if pg_hba.last_backup:
                    ret["backup_file"] = pg_hba.last_backup

    ret["pg_hba"] = [rule for rule in pg_hba.get_rules()]
    module.exit_json(**ret)


# Traceback (nTypeError)
(venv3.6) ubuntu@ubuntu-GB-BXi3-5010:~/ansible$ ansible-playbook ~/ansible/renderworker/postgres/ansible/setup_postgres_pg_hba.yml

PLAY [192.168.1.110] ************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [192.168.1.110]

TASK [Grant postgres access from home] ******************************************************************************************************************************************************************************
fatal: [192.168.1.110]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 192.168.1.110 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1557891828.8283231-200022434247587/AnsiballZ_postgresql_pg_hba.py\", line 125, in <module>\r\n    _ansiballz_main()\r\n  File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1557891828.8283231-200022434247587/AnsiballZ_postgresql_pg_hba.py\", line 117, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1557891828.8283231-200022434247587/AnsiballZ_postgresql_pg_hba.py\", line 51, in invoke_module\r\n    spec.loader.exec_module(module)\r\n  File \"<frozen importlib._bootstrap_external>\", line 678, in exec_module\r\n  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\r\n  File \"/tmp/ansible_postgresql_pg_hba_payload_y9useyqu/__main__.py\", line 709, in <module>\r\n  File \"/tmp/ansible_postgresql_pg_hba_payload_y9useyqu/__main__.py\", line 696, in main\r\n  File \"/tmp/ansible_postgresql_pg_hba_payload_y9useyqu/__main__.py\", line 319, in write\r\n  File \"/tmp/ansible_postgresql_pg_hba_payload_y9useyqu/__main__.py\", line 390, in render\r\n  File \"/tmp/ansible_postgresql_pg_hba_payload_y9useyqu/__main__.py\", line 390, in <listcomp>\r\n  File \"/tmp/ansible_postgresql_pg_hba_payload_y9useyqu/__main__.py\", line 371, in get_rules\r\n  File \"/usr/lib/python3.6/ipaddress.py\", line 652, in __lt__\r\n    self, other))\r\nTypeError: 127.0.0.1/32 and ::1/128 are not of the same version\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

PLAY RECAP **********************************************************************************************************************************************************************************************************
192.168.1.110              : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

# Fixed code
def main():
    """
    This function is the main function of this module
    """
    # argument_spec = postgres_common_argument_spec()
    argument_spec = dict()
    argument_spec.update(
        address=dict(type="str", default="samehost", aliases=["source", "src"]),
        backup_file=dict(type="str"),
        contype=dict(type="str", default=None, choices=PG_HBA_TYPES),
        create=dict(type="bool", default=False),
        databases=dict(type="str", default="all"),
        dest=dict(type="path", required=True),
        method=dict(type="str", default="md5", choices=PG_HBA_METHODS),
        netmask=dict(type="str"),
        options=dict(type="str"),
        order=dict(type="str", default="sdu", choices=PG_HBA_ORDERS),
        state=dict(type="str", default="present", choices=["absent", "present"]),
        users=dict(type="str", default="all"),
    )
    module = AnsibleModule(
        argument_spec=argument_spec, add_file_common_args=True, supports_check_mode=True
    )
    if not HAS_IPADDRESS:
        module.fail_json(
            msg=missing_required_lib("psycopg2"), exception=IPADDRESS_IMP_ERR
        )

    contype = module.params["contype"]
    create = bool(module.params["create"] or module.check_mode)
    if module.check_mode:
        backup = False
    else:
        backup = module.params["backup"]
        backup_file = module.params["backup_file"]
    databases = module.params["databases"]
    dest = module.params["dest"]

    method = module.params["method"]
    netmask = module.params["netmask"]
    options = module.params["options"]
    order = module.params["order"]
    source = module.params["address"]
    state = module.params["state"]
    users = module.params["users"]

    ret = {"msgs": []}
    try:
        pg_hba = PgHba(dest, order, backup=backup, create=create)
    except PgHbaError as error:
        module.fail_json(msg="Error reading file:\n{0}".format(error))

    if contype:
        try:
            for database in databases.split(","):
                for user in users.split(","):
                    rule = PgHbaRule(
                        contype=contype,
                        databases=database,
                        users=user,
                        source=source,
                        netmask=netmask,
                        method=method,
                        options=options,
                        order=order,
                    )
                    if state == "present":
                        ret["msgs"].append("Adding")
                        pg_hba.add_rule(rule)
                    else:
                        ret["msgs"].append("Removing")
                        pg_hba.remove_rule(rule)
        except PgHbaError as error:
            module.fail_json(msg="Error modifying rules:\n{0}".format(error))
        file_args = module.load_file_common_arguments(module.params)
        ret["changed"] = changed = pg_hba.changed()
        if changed:
            ret["msgs"].append("Changed")
            ret["diff"] = pg_hba.diff

            if not module.check_mode:
                ret["msgs"].append("Writing")
                try:
                    if pg_hba.write(backup_file):
                        module.set_fs_attributes_if_different(
                            file_args, True, pg_hba.diff, expand=False
                        )
                except PgHbaError as error:
                    module.fail_json(msg="Error writing file:\n{0}".format(error))
                if pg_hba.last_backup:
                    ret["backup_file"] = pg_hba.last_backup

    ret["pg_hba"] = [rule for rule in pg_hba.get_rules()]
    module.exit_json(**ret)