# unarchive multiple zip files

**URL:** <https://forum.ansible.com/t/unarchive-multiple-zip-files/17753>\
**Category:** Ansible Project\
**Created:** [March 16, 2015, 3:34pm UTC](https://forum.ansible.com/t/unarchive-multiple-zip-files/17753 "2015-03-16T15:34:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Craig\_Simmons](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@Craig\_Simmons](https://forum.ansible.com/u/Craig_Simmons)\
**Post date:** [March 16, 2015, 3:34pm UTC](https://forum.ansible.com/t/unarchive-multiple-zip-files/17753/1 "2015-03-16T15:34:41Z")

</div>

Hi, very new to Ansible and currently translating a simple shell script to a PlayBook. One thing I can’t accomplish is to unarchive multiple ZIP files within the same directory using a wildcard in Ansible. So this works from a shell script:-

unzip ‘/home/installer/Desktop/source/LandJ/Java/\*.zip’ -d ‘/home/installer/Desktop/source/LandJ/Java/’

but this does not in Ansible:-

- name: extract Java  
unarchive: src=/home/installer/Desktop/source/LandJ/Java/\*.zip dest=/home/installer/Desktop/source/LandJ/Java copy=no

running the above generates this error:-

failed: [mfp3] =\> {“failed”: true}  
msg: Source ‘/home/installer/Desktop/source/LandJ/Java/\*.zip’ does not exist

FATAL: all hosts have already failed – aborting

So I tried explicitly referencing both of the ZIP files within the directory like this:-

- name: extract Java  
unarchive: src=/home/installer/Desktop/source/LandJ/Java/zipfile1.zip dest=/home/installer/Desktop/source/LandJ/Java copy=no  
unarchive: src=/home/installer/Desktop/source/LandJ/Java/zipfile2.zip dest=/home/installer/Desktop/source/LandJ/Java copy=no

It seems to unzip the second zip file but neglects to unzip the first.

Finally I ran the unarchive as two separate tasks and this works but this is totally impractical (as is the version above, had it worked) given the number of zip files I’ll be dealing with and the regularity with which the quantity and names will change:-

- name: extract Java zip one  
unarchive: src=/home/installer/Desktop/source/LandJ/Java/zipfile1.zip dest=/home/installer/Desktop/source/LandJ/Java copy=no
- name: extract Java zip two  
unarchive: src=/home/installer/Desktop/source/LandJ/Java/zipfile2.zip dest=/home/installer/Desktop/source/LandJ/Java copy=no

I’m sure I’m doing something fundamentally wrong and it’s me, not Ansible - please feel free to shout at me on your way to pointing out the blindingly obvious …

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 16, 2015, 4:16pm UTC](https://forum.ansible.com/t/unarchive-multiple-zip-files/17753/2 "2015-03-16T16:16:39Z")

</div>

so a couple of issues, most modules will not accept \* or ? as they do  
not shell out your exact line. You can use the fileglob lookup though,  
which does accept this to write:

- name: extract Java  
unarchive: src={{item}} dest=/home/installer/Desktop/source/LandJ/Java copy=no  
with\_fileglob: '/home/installer/Desktop/source/LandJ/Java/\*.zip'

Another issue you were having is putting 2 tasks in one, only one will  
get executed. Each needs to be it's own task, 'name' is an optional  
property not a block indicator, as you found out, but this also works:

- unarchive: src=/home/installer/Desktop/source/LandJ/Java/zipfile1.zip  
dest=/home/installer/Desktop/source/LandJ/Java copy=no

- unarchive: src=/home/installer/Desktop/source/LandJ/Java/zipfile2.zip  
dest=/home/installer/Desktop/source/LandJ/Java copy=no

In any case I think you want the first solution.

---

<div class="post-metadata">

**Author:** ![Craig\_Simmons](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@Craig\_Simmons](https://forum.ansible.com/u/Craig_Simmons)\
**Post date:** [March 17, 2015, 10:00am UTC](https://forum.ansible.com/t/unarchive-multiple-zip-files/17753/3 "2015-03-17T10:00:53Z")

</div>

Hi Brian, thanks for the response. A bit confused about the {{item}} variable you mention? How does that translate to all zip files in the directory? I get the following failure if I run precisely what you’ve suggested, but that may not be your intention?:-

ERROR: Syntax Error while loading YAML script, /home/installer/Desktop/scripts/LandJ/LandJ-Install.yml  
Note: The error may actually appear before this position: line 10, column 99

- name: extract Java ZIP file one on target(s) - this may take some time  
unarchive: src={{item}} dest=/home/installer/Desktop/source/LandJ/Java copy=no with\_fileglob: ‘/home/installer/Desktop/source/LandJ/Java/\*.zip’  
^  
We could be wrong, but this one looks like it might be an issue with  
missing quotes. Always quote template expression brackets when they  
start a value. For instance:

with\_items:

- {{ foo }}

Should be written as:

with\_items:

- “{{ foo }}”

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 17, 2015, 12:54pm UTC](https://forum.ansible.com/t/unarchive-multiple-zip-files/17753/4 "2015-03-17T12:54:05Z")

</div>

so Item is a variable that always gets populated when adding a with\_  
clause, with\_ is how you iterate in a task. with\_ clauses must be on  
their own line, not on the task line, write it as I do here:

- name: extract Java ZIP file one on target(s) - this may take some  
&nbsp;&nbsp;unarchive: src={{item}}  
dest=/home/installer/Desktop/source/LandJ/Java copy=no  
&nbsp;&nbsp;with\_fileglob: '/home/installer/Desktop/source/LandJ/Java/\*.zip'

with\_ clauses use a lookup plugin 'fileglob' is such a plugin, there  
are many others.

more info at:  
[http://docs.ansible.com/playbooks\_loops.html](http://docs.ansible.com/playbooks_loops.html)

---

<div class="post-metadata">

**Author:** ![Craig\_Simmons](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@Craig\_Simmons](https://forum.ansible.com/u/Craig_Simmons)\
**Post date:** [March 17, 2015, 2:54pm UTC](https://forum.ansible.com/t/unarchive-multiple-zip-files/17753/5 "2015-03-17T14:54:30Z")

</div>

Brilliant. Thanks Brian. All working now. Much appreciated
