ansible fetch fails but creates zero-length file

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

Eric,

Can you provide your task? Any error messages?

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

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

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

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. :slight_smile: