# ansible fetch fails but creates zero-length file

**URL:** <https://forum.ansible.com/t/ansible-fetch-fails-but-creates-zero-length-file/28706>\
**Category:** Ansible Project\
**Created:** [October 2, 2018, 5:45pm UTC](https://forum.ansible.com/t/ansible-fetch-fails-but-creates-zero-length-file/28706 "2018-10-02T17:45:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![erichymowitz](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@erichymowitz](https://forum.ansible.com/u/erichymowitz)\
**Post date:** [October 2, 2018, 5:45pm UTC](https://forum.ansible.com/t/ansible-fetch-fails-but-creates-zero-length-file/28706/1 "2018-10-02T17:45:38Z")

</div>

When I use the fetch module (with or without using become, which is contra-indicated but I have no choice), and for whatever reason the file cannot be copied, the local machine still creates a zero-length file. Is there a way to disable this behavior?

Thanks.

–EbH

---

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [October 3, 2018, 12:44pm UTC](https://forum.ansible.com/t/ansible-fetch-fails-but-creates-zero-length-file/28706/2 "2018-10-03T12:44:55Z")

</div>

Eric,

Can you provide your task? Any error messages?

---

<div class="post-metadata">

**Author:** ![erichymowitz](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@erichymowitz](https://forum.ansible.com/u/erichymowitz)\
**Post date:** [October 3, 2018, 1:20pm UTC](https://forum.ansible.com/t/ansible-fetch-fails-but-creates-zero-length-file/28706/3 "2018-10-03T13:20:32Z")

</div>

I made a mistake – it’s not “for whatever reason the file cannot be copied” … but it happens (at least) when I can’t open the remote file (permission denied).

[ehymowitz test]$ ls -al cougar

total 8,192

drwx------. 2 ehymowitz ehymowitz 4,096 Oct 3 13:15 .

drwx------. 3 ehymowitz ehymowitz 4,096 Oct 3 13:13 …

[ehymowitz test]$ cat fetch.yaml

- hosts: cougar

gather\_facts: no

tasks:

- name: fetch

fetch:

src: /etc/sysconfig/iptables

dest: “/home/ehymowitz/ansible/test/cougar/{{inventory\_hostname}}.iptables”

flat: yes

[ehymowitz test]$ ansible-playbook fetch.yaml

PLAY [cougar] \*\*\*\*\*\*

TASK [fetch] \*\*\*\*\*\*\*

[WARNING]: sftp transfer mechanism failed on [cougar]. Use ANSIBLE\_DEBUG=1 to

see detailed information

[WARNING]: scp transfer mechanism failed on [cougar]. Use ANSIBLE\_DEBUG=1 to

see detailed information

[WARNING]: piped transfer mechanism failed on [cougar]. Use ANSIBLE\_DEBUG=1 to

see detailed information

fatal: [cougar]: FAILED! =\> {“msg”:

"failed to transfer file to /etc/sysconfig/iptables /home/ehymowitz/ansible/test/cougar/cougar.iptables:\n\ndd:

opening `/etc/sysconfig/iptables’: Permission denied\n"}

to retry, use: --limit @/home/ehymowitz/ansible/test/fetch.retry

PLAY RECAP \*\*\*\*\*\*\*

cougar : ok=0 changed=0 unreachable=0 failed=1

[ehymowitz test]$ ls -al cougar

total 8,192

drwx------. 2 ehymowitz ehymowitz 4,096 Oct 3 13:15 .

drwx------. 3 ehymowitz ehymowitz 4,096 Oct 3 13:13 …

-rw-------. 1 ehymowitz ehymowitz 0 Oct 3 13:15 cougar.iptables

–EbH

---

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [October 3, 2018, 2:20pm UTC](https://forum.ansible.com/t/ansible-fetch-fails-but-creates-zero-length-file/28706/4 "2018-10-03T14:20:43Z")

</div>

I think not using become is what is killing you. iptables is definitely only readable by root. Try turning “become” back on.

---

<div class="post-metadata">

**Author:** ![erichymowitz](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@erichymowitz](https://forum.ansible.com/u/erichymowitz)\
**Post date:** [October 3, 2018, 2:33pm UTC](https://forum.ansible.com/t/ansible-fetch-fails-but-creates-zero-length-file/28706/5 "2018-10-03T14:33:25Z")

</div>

My problem isn’t “the file didn’t copy”. I know why the file didn’t copy.

The question is, why did a zero-length destination file appear when the file didn’t copy? If the file didn’t copy, then my destination should not exist.

–EbH

---

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [October 3, 2018, 2:37pm UTC](https://forum.ansible.com/t/ansible-fetch-fails-but-creates-zero-length-file/28706/6 "2018-10-03T14:37:34Z")

</div>

Not necessarily. Depends on the order of execution within the module. It might “touch” the destination before attempting the copy and may not delete it if the copy fails. Not sure. You’d have to look at the source code of the fetch module. 🙂
