# Buggy code
def __init__(
    self,
    contype=None,
    databases=None,
    users=None,
    source=None,
    netmask=None,
    method=None,
    options=None,
    line=None,
):
    """
    This function can be called with a comma seperated list of databases and a comma seperated
    list of users and it will act as a generator that returns a expanded list of rules one by
    one.
    """

    super(PgHbaRule, self).__init__()

    if line:
        # Read valies from line if parsed
        self.fromline(line)

    # read rule cols from parsed items
    rule = dict(
        zip(PG_HBA_HDR, [contype, databases, users, source, netmask, method, options])
    )
    for key, value in rule.items():
        if value:
            self[key] = value

    # Some sanity checks
    for key in ["method", "type"]:
        if key not in self:
            raise PgHbaRuleError("Missing {0} in rule {1}".format(key, self))

    if self["method"] not in PG_HBA_METHODS:
        msg = "invalid method {0} (should be one of '{1}')."
        raise PgHbaRuleValueError(
            msg.format(self["method"], "', '".join(PG_HBA_METHODS))
        )

    if self["type"] not in PG_HBA_TYPES:
        msg = "invalid connection type {0} (should be one of '{1}')."
        raise PgHbaRuleValueError(msg.format(self["type"], "', '".join(PG_HBA_TYPES)))

    if self["type"] == "local":
        self.unset("src")
        self.unset("mask")
    elif "src" not in self:
        raise PgHbaRuleError("Missing src in rule {1}".format(self))
    elif "/" in self["src"]:
        self.unset("mask")
    else:
        self["src"] = str(self.source())
        self.unset("mask")


# 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 __init__(
    self,
    contype=None,
    databases=None,
    users=None,
    source=None,
    netmask=None,
    method=None,
    options=None,
    line=None,
    order="sdu",
):
    """
    This function can be called with a comma seperated list of databases and a comma seperated
    list of users and it will act as a generator that returns a expanded list of rules one by
    one.
    """

    super(PgHbaRule, self).__init__()
    if order not in PG_HBA_ORDERS:
        msg = "invalid order setting {0} (should be one of '{1}')."
        raise PgHbaError(msg.format(order, "', '".join(PG_HBA_ORDERS)))
    self.order = order

    if line:
        # Read valies from line if parsed
        self.fromline(line)

    # read rule cols from parsed items
    rule = dict(
        zip(PG_HBA_HDR, [contype, databases, users, source, netmask, method, options])
    )
    for key, value in rule.items():
        if value:
            self[key] = value

    # Some sanity checks
    for key in ["method", "type"]:
        if key not in self:
            raise PgHbaRuleError("Missing {0} in rule {1}".format(key, self))

    if self["method"] not in PG_HBA_METHODS:
        msg = "invalid method {0} (should be one of '{1}')."
        raise PgHbaRuleValueError(
            msg.format(self["method"], "', '".join(PG_HBA_METHODS))
        )

    if self["type"] not in PG_HBA_TYPES:
        msg = "invalid connection type {0} (should be one of '{1}')."
        raise PgHbaRuleValueError(msg.format(self["type"], "', '".join(PG_HBA_TYPES)))

    if self["type"] == "local":
        self.unset("src")
        self.unset("mask")
    elif "src" not in self:
        raise PgHbaRuleError("Missing src in rule {1}".format(self))
    elif "/" in self["src"]:
        self.unset("mask")
    else:
        self["src"] = str(self.source())
        self.unset("mask")