# Buggy code
def return_values(obj):
    """Return stringified values from datastructures. For use with removing
    sensitive values pre-jsonification."""
    if isinstance(obj, basestring):
        if obj:
            yield obj
        return
    elif isinstance(obj, Sequence):
        for element in obj:
            for subelement in return_values(element):
                yield subelement
    elif isinstance(obj, Mapping):
        for element in obj.items():
            for subelement in return_values(element[1]):
                yield subelement
    elif isinstance(obj, (bool, NoneType)):
        # This must come before int because bools are also ints
        return
    elif isinstance(obj, NUMBERTYPES):
        yield str(obj)
    else:
        raise TypeError("Unknown parameter type: %s, %s" % (type(obj), obj))


# 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 return_values(obj):
    """Return stringified values from datastructures. For use with removing
    sensitive values pre-jsonification."""
    if isinstance(obj, basestring):
        if obj:
            if isinstance(obj, bytes):
                yield obj
            else:
                # Unicode objects should all convert to utf-8
                # (still must deal with surrogateescape on python3)
                yield obj.encode("utf-8")
        return
    elif isinstance(obj, Sequence):
        for element in obj:
            for subelement in return_values(element):
                yield subelement
    elif isinstance(obj, Mapping):
        for element in obj.items():
            for subelement in return_values(element[1]):
                yield subelement
    elif isinstance(obj, (bool, NoneType)):
        # This must come before int because bools are also ints
        return
    elif isinstance(obj, NUMBERTYPES):
        yield str(obj)
    else:
        raise TypeError("Unknown parameter type: %s, %s" % (type(obj), obj))