# Buggy code
def is_encrypted(self, data):
    """Test if this is vault encrypted data

    :arg data: a byte str or unicode string to test whether it is
        recognized as vault encrypted data
    :returns: True if it is recognized.  Otherwise, False.
    """

    if to_bytes(data, errors="strict", encoding="utf-8").startswith(b_HEADER):
        return True
    return False


# Traceback (MemoryError)
$ ansible-playbook -i hosts huge_copy.yml -vvv
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: huge_copy.yml ********************************************************
1 plays in huge_copy.yml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
<192.168.80.129> ESTABLISH SSH CONNECTION FOR USER: ansible
<192.168.80.129> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=10 -o ControlPath=/home/y/.ansible/cp/ansible-ssh-%h-%p-%r 192.168.80.129 '/bin/sh -c '"'"'( umask 77 &amp;&amp; mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1466567672.43-71840509019686 `" &amp;&amp; echo ansible-tmp-1466567672.43-71840509019686="` echo $HOME/.ansible/tmp/ansible-tmp-1466567672.43-71840509019686 `" ) &amp;&amp; sleep 0'"'"''
<192.168.80.129> PUT /tmp/tmp3Bk56k TO /home/ansible/.ansible/tmp/ansible-tmp-1466567672.43-71840509019686/setup
<192.168.80.129> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=10 -o ControlPath=/home/y/.ansible/cp/ansible-ssh-%h-%p-%r '[192.168.80.129]'
<192.168.80.129> ESTABLISH SSH CONNECTION FOR USER: ansible
<192.168.80.129> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=10 -o ControlPath=/home/y/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.80.129 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-rmwgnaejatjdbjwnltokljxsdjjlhagx; LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1466567672.43-71840509019686/setup; rm -rf "/home/ansible/.ansible/tmp/ansible-tmp-1466567672.43-71840509019686/" > /dev/null 2>&amp;1'"'"'"'"'"'"'"'"' &amp;&amp; sleep 0'"'"''
ok: [192.168.80.129]

TASK [Copy huge file] **********************************************************
task path: /home/y/nfvdv4-single/huge_copy.yml:6
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/ansible/executor/task_executor.py", line 124, in run
res = self._execute()
File "/usr/lib/python2.7/dist-packages/ansible/executor/task_executor.py", line 446, in _execute
result = self._handler.run(task_vars=variables)
File "/usr/lib/python2.7/dist-packages/ansible/plugins/action/copy.py", line 156, in run
source_full = self._loader.get_real_file(source_full)
File "/usr/lib/python2.7/dist-packages/ansible/parsing/dataloader.py", line 334, in get_real_file
data = f.read()
MemoryError

fatal: [192.168.80.129]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}

NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @huge_copy.retry

PLAY RECAP *********************************************************************
192.168.80.129             : ok=1    changed=0    unreachable=0    failed=1

# Fixed code
def is_encrypted(self, data):
    """Test if this is vault encrypted data

    :arg data: a byte str or unicode string to test whether it is
        recognized as vault encrypted data
    :returns: True if it is recognized.  Otherwise, False.
    """

    if hasattr(data, "read"):
        current_position = data.tell()
        header_part = data.read(len(b_HEADER))
        data.seek(current_position)
        return self.is_encrypted(header_part)

    if to_bytes(data, errors="strict", encoding="utf-8").startswith(b_HEADER):
        return True
    return False