Skip to content
Advertisement

How to remove temp file in ansible using copy module

I’m using adhoc command for example

ansible some_group_of_hosts -m copy -a "src=/some_local_file
dest=/remote_path/"  -u someuser --become-user root --ask-pass
--ask-become-pass

After copy in the remote host. I found temp files in the directory ~/.ansible/tmp

How to remove this temp directory on remote host? Using another module like “commmand” ? In the reference of copy ansible module i did not found about remove temp file after copy.

Advertisement

Answer

~/.ansible/tmp is a “working” directory for Ansible on remote host.
As far as I know, it is not supposed to be removed.
For each task Ansible creates ~/.ansible/tmp/ansible-tmp-12345.1234.12345 directory and then cleans up only ansible-tmp-xxx subdirectory, and not “root” working directory.

You can change this remote path with remote_tmp setting or with environment variable, like:

ANSIBLE_REMOTE_TEMP=/tmp ansible some_group_of_hosts -m copy -a "src=/some_local_file dest=/remote_path/" -u someuser --become-user root --ask-pass --ask-become-pass

This will force Ansible to work in /tmp and not create ~/.ansible/tmp. But there’s a risk of someone intercepting temporary data inside common /tmp directory.

Advertisement