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 …
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.
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:
Should be written as:
with_items:
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
Brilliant. Thanks Brian. All working now. Much appreciated