# unarchive is not skipping if the folder already exists

**URL:** <https://forum.ansible.com/t/unarchive-is-not-skipping-if-the-folder-already-exists/29123>\
**Category:** Ansible Project\
**Created:** [November 27, 2018, 8:52am UTC](https://forum.ansible.com/t/unarchive-is-not-skipping-if-the-folder-already-exists/29123 "2018-11-27T08:52:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 27, 2018, 8:52am UTC](https://forum.ansible.com/t/unarchive-is-not-skipping-if-the-folder-already-exists/29123/1 "2018-11-27T08:52:01Z")

</div>

Hi Experts,

I have below play book for extracting a .zip file from Ansible controller machine to the target folders. But i noticed that play is not working as expected if the target folder and the unzipped files are already present in the target servers. Could you please guide me here ?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 27, 2018, 11:28am UTC](https://forum.ansible.com/t/unarchive-is-not-skipping-if-the-folder-already-exists/29123/2 "2018-11-27T11:28:01Z")

</div>

> Hi Experts,
> 
> I have below play book for extracting a .zip file from Ansible controller machine to the target folders. But i noticed that play is not working as expected if the target folder and the unzipped files are already present in the target servers. Could you please guide me here ?

Hi

Some questions:

1. The playbook is restricted to localhost, and 'extracting a .zip  
file from Ansible controller machine to the target folders' also  
indicates you want to extract a ZIP file on the controller machine.  
But then your talk about 'files are already present in the target  
servers' - which indicates that you DO want to extract the archive on  
a remote host. What do you actually mean?

2. You mention that the play 'is not working as expected'. The  
question then is: what \_did\_ you expect (and what exactly isn't going  
as you expected)?  
3. You now use a fragile combination of two modules (stat + command)  
to achieve what can also be done with only one module: file.  
4. The 'creates' parameter of the unarchive module should be an  
absolute path, but you specify it as 'no'.  
5. Cosmetically: you define 'source\_dir' but then the value is the  
path of a (ZIP) file. Strictly speaking there is nothing wrong, but it  
does make things confusing.  
6. If this is part of a role (which  
'/etc/ansible/roles/IBM2/files/mypack.zip' indicates), you could leave  
out the full path as this will be implicit (i.e. just  
'files/mypack.zip')

The good news is that, depending on what you really want (still  
unclear at this point), it could be as simple as use a single  
unarchive task.

At the risk of guessing:

- name: Extract file  
&nbsp;&nbsp;hosts: targethosts  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;zipfile: files/mypack.zip  
&nbsp;&nbsp;&nbsp;&nbsp;destdir: /opt/mypack  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: extract zip file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unarchive:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ zipfile }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ destdir }}"

Dick

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 27, 2018, 12:57pm UTC](https://forum.ansible.com/t/unarchive-is-not-skipping-if-the-folder-already-exists/29123/3 "2018-11-27T12:57:23Z")

</div>

Hello Dick,

Thanks for all your suggestions.

I am very sorry for the mistake in my playbook which i copied here. Actually as i stated i would need to extract the mypack.zip file which is in the controller machine to all my application servers( here by mistake i defined as localhost, where as a list of servers are there).

my aim is that extract the .zip file in the controller to the specific folder in application servers (because i have another script with this path specified for calling the package).

i was trying for 2 conditions

1. check the target servers directory present or not (eg: /opt/mypack). if it present skip else create directory.
2. check the above directory already have the extracted file or not. if no extract, if yes skip extract.

I tried with below playbook where i want to extract 2 .zip packages to all target servers specific location (/opt/mypack and /opt/mypack2)

---

<div class="post-metadata">

**Author:** ![Bharath\_Kumar2](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@Bharath\_Kumar2](https://forum.ansible.com/u/Bharath_Kumar2)\
**Post date:** [November 28, 2018, 3:53am UTC](https://forum.ansible.com/t/unarchive-is-not-skipping-if-the-folder-already-exists/29123/4 "2018-11-28T03:53:38Z")

</div>

Try the below (check for indentation)

`

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 28, 2018, 8:17am UTC](https://forum.ansible.com/t/unarchive-is-not-skipping-if-the-folder-already-exists/29123/5 "2018-11-28T08:17:26Z")

</div>

Hello Bharath,

Thanks for your suggestion and i tried with below play and it didnt get succeed.

play
