# Buggy code
def write(self, backup_file=""):
    """
    This method writes the PgHba rules (back) to a file.
    """
    if not self.changed():
        return False

    if self.pg_hba_file:
        if not (os.path.isfile(self.pg_hba_file) or self.create):
            raise PgHbaError(
                "pg_hba file '{0}' doesn't exist. "
                "Use create option to autocreate.".format(self.pg_hba_file)
            )
        if self.backup and os.path.isfile(self.pg_hba_file):
            if backup_file:
                self.last_backup = backup_file
            else:
                __backup_file_h, self.last_backup = tempfile.mkstemp(prefix="pg_hba")
            shutil.copy(self.pg_hba_file, self.last_backup)
        fileh = open(self.pg_hba_file, "w")
    else:
        filed, __path = tempfile.mkstemp(prefix="pg_hba")
        fileh = os.fdopen(filed, "w")

    fileh.write(self.render())
    self.unchanged()
    fileh.close()
    return True


# 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 write(self, backup_file=""):
    """
    This method writes the PgHba rules (back) to a file.
    """
    if not self.changed():
        return False

    contents = self.render()
    if self.pg_hba_file:
        if not (os.path.isfile(self.pg_hba_file) or self.create):
            raise PgHbaError(
                "pg_hba file '{0}' doesn't exist. "
                "Use create option to autocreate.".format(self.pg_hba_file)
            )
        if self.backup and os.path.isfile(self.pg_hba_file):
            if backup_file:
                self.last_backup = backup_file
            else:
                __backup_file_h, self.last_backup = tempfile.mkstemp(prefix="pg_hba")
            shutil.copy(self.pg_hba_file, self.last_backup)
        fileh = open(self.pg_hba_file, "w")
    else:
        filed, __path = tempfile.mkstemp(prefix="pg_hba")
        fileh = os.fdopen(filed, "w")

    fileh.write(contents)
    self.unchanged()
    fileh.close()
    return True