# Buggy code
def get_real_file(self, file_path):
    """
    If the file is vault encrypted return a path to a temporary decrypted file
    If the file is not encrypted then the path is returned
    Temporary files are cleanup in the destructor
    """

    if not file_path or not isinstance(file_path, string_types):
        raise AnsibleParserError("Invalid filename: '%s'" % str(file_path))

    if not self.path_exists(file_path) or not self.is_file(file_path):
        raise AnsibleFileNotFound(
            "the file_name '%s' does not exist, or is not readable" % file_path
        )

    if not self._vault:
        self._vault = VaultLib(password="")

    real_path = self.path_dwim(file_path)

    try:
        with open(to_bytes(real_path), "rb") as f:
            data = f.read()
            if self._vault.is_encrypted(data):
                # if the file is encrypted and no password was specified,
                # the decrypt call would throw an error, but we check first
                # since the decrypt function doesn't know the file name
                if not self._vault_password:
                    raise AnsibleParserError(
                        "A vault password must be specified to decrypt %s" % file_path
                    )

                data = self._vault.decrypt(data, filename=real_path)
                # Make a temp file
                real_path = self._create_content_tempfile(data)
                self._tempfiles.add(real_path)

        return real_path

    except (IOError, OSError) as e:
        raise AnsibleParserError(
            "an error occurred while trying to read the file '%s': %s"
            % (real_path, str(e))
        )


# 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 get_real_file(self, file_path):
    """
    If the file is vault encrypted return a path to a temporary decrypted file
    If the file is not encrypted then the path is returned
    Temporary files are cleanup in the destructor
    """

    if not file_path or not isinstance(file_path, string_types):
        raise AnsibleParserError("Invalid filename: '%s'" % str(file_path))

    if not self.path_exists(file_path) or not self.is_file(file_path):
        raise AnsibleFileNotFound(
            "the file_name '%s' does not exist, or is not readable" % file_path
        )

    if not self._vault:
        self._vault = VaultLib(password="")

    real_path = self.path_dwim(file_path)

    try:
        with open(to_bytes(real_path), "rb") as f:
            if self._vault.is_encrypted(f):
                # if the file is encrypted and no password was specified,
                # the decrypt call would throw an error, but we check first
                # since the decrypt function doesn't know the file name
                data = f.read()
                if not self._vault_password:
                    raise AnsibleParserError(
                        "A vault password must be specified to decrypt %s" % file_path
                    )

                data = self._vault.decrypt(data, filename=real_path)
                # Make a temp file
                real_path = self._create_content_tempfile(data)
                self._tempfiles.add(real_path)

        return real_path

    except (IOError, OSError) as e:
        raise AnsibleParserError(
            "an error occurred while trying to read the file '%s': %s"
            % (real_path, str(e))
        )