Ansible: unpack a file on the remote host

Posted by ads' corner on Wednesday, 2016-08-24
Posted in [Ansible][Linux]

Trying to unpack a file on the remote host using Ansible might end in an error message:

fatal: [192.168.0.188]: FAILED! => {"failed": true, "msg": "ERROR! file or module does not exist: /tmp/test.zip"}

I’m using Ansible 2.0.0.2 here, and have previously copied test.zip to the host (yes, I know, the unarchive module can do the copy as well). Now I’m trying to unpack it:

1
2
- name: unzip test.zip archive
  unarchive: src=/tmp/test.zip dest=/tmp/test-tmp-install remote_src=yes

This fails, even though I specify remote_src and tell Ansible that the file is already on the remote host.

The solution is easy, and braindead: there is another unarchive parameter named copy. The default setting is yes.

The documentation says that it is deprecated, and mutually exclusive to remote_src. But still, not setting copy=no will result in Ansible looking for the file on your Ansible host, and failing miserable if it’s not there. However, if the file is available on your Ansible host (think roles and the files directories), it will happily copy the file, and therefore overwrite your destination file with whatever is found on your Ansible host.

The correct Playbook statement is:

1
2
- name: unzip test archive
  unarchive: src=/tmp/test.zip dest=/tmp/test-tmp-install remote_src=yes copy=no

Update: see the updated posting for Ansible >= 2.2


Categories: [Ansible] [Linux]
Tags: [Ansible] [Automation] [Linux] [Zip]