unarchive multiple zip files

3,643 views
Skip to first unread message

tsg07ica

unread,
Mar 16, 2015, 11:47:21 AM3/16/15
to ansible...@googlegroups.com
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 ....

Brian Coca

unread,
Mar 16, 2015, 12:16:41 PM3/16/15
to ansible...@googlegroups.com
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.


--
Brian Coca

tsg07ica

unread,
Mar 17, 2015, 6:00:53 AM3/17/15
to ansible...@googlegroups.com
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 }}" 

Brian Coca

unread,
Mar 17, 2015, 8:54:08 AM3/17/15
to ansible...@googlegroups.com
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
unarchive: src={{item}}
dest=/home/installer/Desktop/source/LandJ/Java copy=no
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


--
Brian Coca

Craig Simmons

unread,
Mar 17, 2015, 10:54:42 AM3/17/15
to ansible...@googlegroups.com
Brilliant. Thanks Brian. All working now. Much appreciated
> --
> You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/hcTCGqPtzvM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8nWLBGfvnojHFcZFpNh1R9oBiTEX0gN84eF1jKpR31rMw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages