unarchive is not skipping if the folder already exists

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 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
  hosts: targethosts
  vars:
    zipfile: files/mypack.zip
    destdir: /opt/mypack
  tasks:
    - name: extract zip file
      unarchive:
        src: "{{ zipfile }}"
        dest: "{{ destdir }}"

Dick

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)

Try the below (check for indentation)

`

Hello Bharath,

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

play