# Buggy code
def remove_values(value, no_log_strings):
    """Remove strings in no_log_strings from value.  If value is a container
    type, then remove a lot more"""
    if isinstance(value, basestring):
        if value in no_log_strings:
            return "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
        for omit_me in no_log_strings:
            value = value.replace(omit_me, "*" * 8)
    elif isinstance(value, Sequence):
        return [remove_values(elem, no_log_strings) for elem in value]
    elif isinstance(value, Mapping):
        return dict((k, remove_values(v, no_log_strings)) for k, v in value.items())
    elif isinstance(value, tuple(chain(NUMBERTYPES, (bool, NoneType)))):
        stringy_value = str(value)
        if stringy_value in no_log_strings:
            return "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
        for omit_me in no_log_strings:
            if omit_me in stringy_value:
                return "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
    else:
        raise TypeError("Value of unknown type: %s, %s" % (type(value), value))
    return value


# Traceback (UnicodeDecodeError)
TASK [incidentresponse : raw] **************************************************
task path: /home/julien/Documents/script/homelab/roles/incidentresponse/tasks/windows-cmds.yml:7
<192.168.2.109> ESTABLISH WINRM CONNECTION FOR USER: user on PORT 5986 TO 192.168.2.109
<192.168.2.109> EXEC netstat -anoq
ok: [192.168.2.109] => {"changed": false, "invocation": {"module_args": {"_raw_params": "netstat -anoq"}, "module_name": "raw"}, "rc": 0, "stderr": "", "stdout": "\r\nConnexions actives\r\n\r\n  Proto  Adresse locale         Adresse distante       �tat\r\n  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       624\r\n  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4\r\n  TCP    0.0.0.0:5357           0.0.0.0:0              LISTENING       4\r\n  TCP    0.0.0.0:5985           0.0.0.0:0              LISTENING       4\r\n  TCP    0.0.0.0:5986           0.0.0.0:0              LISTENING       4\r\n  TCP    0.0.0.0:7680           0.0.0.0:0              LISTENING       764\r\n  TCP    0.0.0.0:47001          0.0.0.0:0              LISTENING       4\r\n  TCP    0.0.0.0:49408          0.0.0.0:0              LISTENING       408\r\n  TCP    0.0.0.0:49409          0.0.0.0:0              LISTENING       764\r\n  TCP    0.0.0.0:49410          0.0.0.0:0              LISTENING       280\r\n  TCP    0.0.0.0:49411
[...]

TASK [incidentresponse : copy content={{ netstatanoq }} dest=/tmp/{{ prefix }}-netstat-anoq] ***
task path: /home/julien/Documents/script/homelab/roles/incidentresponse/tasks/windows-cmds.yml:9
ESTABLISH LOCAL CONNECTION FOR USER: julien
localhost EXEC rc=flag; [ -r /tmp/DESKTOP-PTPUFI8-netstat-anoq ] || rc=2; [ -f /tmp/DESKTOP-PTPUFI8-netstat-anoq ] || rc=1; [ -d /tmp/DESKTOP-PTPUFI8-netstat-anoq ] &amp;&amp; rc=3; python -V 2>/dev/null || rc=4; [ x"$rc" != "xflag" ] &amp;&amp; echo "${rc}  "/tmp/DESKTOP-PTPUFI8-netstat-anoq &amp;&amp; exit 0; (python -c 'import hashlib; BLOCKSIZE = 65536; hasher = hashlib.sha1();
afile = open("'/tmp/DESKTOP-PTPUFI8-netstat-anoq'", "rb")
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
afile.close()
print(hasher.hexdigest())' 2>/dev/null) || (python -c 'import sha; BLOCKSIZE = 65536; hasher = sha.sha();
afile = open("'/tmp/DESKTOP-PTPUFI8-netstat-anoq'", "rb")
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
afile.close()
print(hasher.hexdigest())' 2>/dev/null) || (echo '0  '/tmp/DESKTOP-PTPUFI8-netstat-anoq)
localhost EXEC (umask 22 &amp;&amp; mkdir -p "`echo $HOME/.ansible/tmp/ansible-tmp-1449018691.64-133920109984120`" &amp;&amp; echo "`echo $HOME/.ansible/tmp/ansible-tmp-1449018691.64-133920109984120`")
localhost PUT /tmp/tmpNONK30 TO /home/julien/.ansible/tmp/ansible-tmp-1449018691.64-133920109984120/source
localhost EXEC (umask 22 &amp;&amp; mkdir -p "`echo $HOME/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930`" &amp;&amp; echo "`echo $HOME/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930`")
localhost PUT /tmp/tmpR40NWl TO /home/julien/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930/copy
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/julien/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930/copy; rm -rf "/home/julien/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930/" > /dev/null 2>&amp;1
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/home/julien/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930/copy", line 2245, in <module>
main()
File "/home/julien/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930/copy", line 222, in main
supports_check_mode=True,
File "/home/julien/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930/copy", line 895, in __init__
self._log_invocation()
File "/home/julien/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930/copy", line 1751, in _log_invocation
self.log(msg, log_args=log_args)
File "/home/julien/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930/copy", line 1692, in log
journal_msg = remove_values(msg.decode('utf-8', 'replace'), self.no_log_values)
File "/home/julien/.ansible/tmp/ansible-tmp-1449018691.64-77202891227930/copy", line 734, in remove_values
value = value.replace(omit_me, '*' * 8)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 79: ordinal not in range(128)

fatal: [192.168.2.109 -> localhost]: FAILED! => {"changed": false, "checksum": "51bce163c2aec0c18fab61ef9e8c3bc616fda0b3", "failed": true, "invocation": {"module_args": {"content": {"changed": false, "rc": 0, "stderr": "", "stdout": "\r\nConnexions actives\r\n\r\n  Proto  Adresse locale         Adresse distante       �tat\r\n  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       624\r\n  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4\r\n  TCP    0.0.0.0:5357           0.0.0.0:0              LISTENING       4\r\n  TCP    0.0.0.0:5985           0.0.0.0:0              LISTENING       4\r\n  TCP    0.0.0.0:5986           0.0.0.0:0              LISTENING       4\r\n  TCP    0.0.0.0:7680           0.0.0.0:0
[...]
%6]:546  *:*                                    280", "  UDP    [fe80::9d6b:93db:7dc1:bd34%6]:1900  *:*                                    292", "  UDP    [fe80::9d6b:93db:7dc1:bd34%6]:58100  *:*                                    292"]}, "dest": "/tmp/DESKTOP-PTPUFI8-netstat-anoq"}, "module_name": "copy"}, "parsed": false}

# Fixed code
def remove_values(value, no_log_strings):
    """Remove strings in no_log_strings from value.  If value is a container
    type, then remove a lot more"""
    if isinstance(value, basestring):
        if isinstance(value, unicode):
            # This should work everywhere on python2. Need to check
            # surrogateescape on python3
            bytes_value = value.encode("utf-8")
            value_is_unicode = True
        else:
            bytes_value = value
            value_is_unicode = False
        if bytes_value in no_log_strings:
            return "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
        for omit_me in no_log_strings:
            bytes_value = bytes_value.replace(omit_me, "*" * 8)
        if value_is_unicode:
            value = unicode(bytes_value, "utf-8", errors="replace")
        else:
            value = bytes_value
    elif isinstance(value, Sequence):
        return [remove_values(elem, no_log_strings) for elem in value]
    elif isinstance(value, Mapping):
        return dict((k, remove_values(v, no_log_strings)) for k, v in value.items())
    elif isinstance(value, tuple(chain(NUMBERTYPES, (bool, NoneType)))):
        stringy_value = str(value)
        if stringy_value in no_log_strings:
            return "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
        for omit_me in no_log_strings:
            if omit_me in stringy_value:
                return "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
    else:
        raise TypeError("Value of unknown type: %s, %s" % (type(value), value))
    return value