unarchive is not skipping if the folder already exists

58 views
Skip to first unread message

visar

unread,
Nov 27, 2018, 3:52:01 AM11/27/18
to Ansible Project
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 ?

############################################################

---

- name: Extract mypack.zip into /opt/mypack
  hosts: localhost
  vars:
    source_dir: /etc/ansible/roles/IBM2/files/mypack.zip
    dest_dir: /opt/mypack
  tasks:
    - name: check the folder existance
      stat: path=/opt/mypack
      register: folder_exist

    - name: create directory
      command: mkdir /opt/mypack
      when: folder_exist.stat.exists == False

    - name: extract the .zip file
      unarchive:
         src: "{{ source_dir }}"
         dest: "{{ dest_dir }}/"
         creates: no
      when: folder_exist.stat.exists == True

#####################################################################

Dick Visser

unread,
Nov 27, 2018, 6:28:26 AM11/27/18
to ansible...@googlegroups.com
On Tue, 27 Nov 2018 at 09:52, visar <vivu...@gmail.com> wrote:
>
> 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



>
> ############################################################
>
> ---
>
> - name: Extract mypack.zip into /opt/mypack
> hosts: localhost
> vars:
> source_dir: /etc/ansible/roles/IBM2/files/mypack.zip
> dest_dir: /opt/mypack
> tasks:
> - name: check the folder existance
> stat: path=/opt/mypack
> register: folder_exist
>
> - name: create directory
> command: mkdir /opt/mypack
> when: folder_exist.stat.exists == False
>
> - name: extract the .zip file
> unarchive:
> src: "{{ source_dir }}"
> dest: "{{ dest_dir }}/"
> creates: no
> when: folder_exist.stat.exists == True
>
> #####################################################################
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, 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/09e072d7-a3f6-485a-8e9b-fb8d8497228b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

visar

unread,
Nov 27, 2018, 7:57:23 AM11/27/18
to Ansible Project
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)


---

- name: Extract CLM-Web Package into /opt/mypack
  hosts: localhost,IHS,CCM,RM
  vars:
    source_dir: /etc/ansible/roles/IBM2/files/mypack.zip
    dest_dir: /opt/mypack
  tasks:
    - name: check the folder existance
      stat: path=/opt/mypack
      register: folder_exist

    - name: create directory
      command: mkdir /opt/mypack
      when: folder_exist.stat.exists == False

    - name: extract the .zip file
      unarchive:
         src: "{{ source_dir }}"
         dest: "{{ dest_dir }}/"
      when: folder_exist.stat.exists == True

- name: Extract CLM Installation Package to /opt/mypack
  hosts: localhost,CCM,RM
  vars:
    source_dir: /etc/ansible/roles/IBM2/files/mypack2.zip
    dest_dir: /opt/mypack2
  tasks:
    - name: check the folder existance
      stat: path=/opt/mypack2
      register: folder_exist

    - name: create directory
      command: mkdir /opt/mypack2
      when: folder_exist.stat.exists == False

    - name: extract the .zip file
      unarchive:
         src: "{{ source_dir }}"
         dest: "{{ dest_dir }}/"
      when: folder_exist.stat.exists == True

Bharath Kumar

unread,
Nov 27, 2018, 10:53:38 PM11/27/18
to Ansible Project
Try the below (check for indentation) 

---
- hosts: all
  gather_facts
: no
  tasks
:
   
- name: Ensure the folder exists!
      file
:
       path
: /opt/mypack
       state
: directory
       mode
: 0755
   
- name: Unpack/Unarchive the .zip file!
      unarchive
:
       src
: {{ item }}
       dest
: /opt/mypack
       remote_src
: no
       with_items
:
           
- /etc/ansible/roles/IBM2/files/mypack.zip
           
- /etc/ansible/roles/IBM2/files/mypack2.zip

visar

unread,
Nov 28, 2018, 3:17:27 AM11/28/18
to Ansible Project
Hello Bharath,

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

play
_________

---

- name: extract the packages
  hosts: localhost,CCM
  gather_facts: no
  tasks:
    - name: Ensure the folder exists!
      file:
       path: /opt/mypack1
       state: directory
       mode: 0755
    - name: Unpack/Unarchive the .zip file!
      unarchive:
       src: "{{ item }}"
       dest: /opt/mypack1
       remote_src: no
       with_items:
           - /etc/ansible/roles/IBM2/files/mypack1.zip
           - /etc/ansible/roles/IBM2/files/mypack2.zip

error
_____
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to have been in '/etc/ansible/roles/IBM2/tasks/good/unarchive2.yml': line 12, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n       mode: 0755\n    - name: Unpack/Unarchive the .zip file!\n      ^ here\n"}
fatal: [10.50.3.1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to have been in '/etc/ansible/roles/IBM2/tasks/good/unarchive2.yml': line 12, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n       mode: 0755\n    - name: Unpack/Unarchive the .zip file!\n      ^ here\n"}


Note:- here i was trying to unzip my two .zip packages to 2 different directory (like mypack1.zip to /opt/mypack1 and mypack2.zip to /opt/mypack2)
Reply all
Reply to author
Forward
0 new messages