would like to skip unzip if directory exist

34 views
Skip to first unread message

jerome...@gmail.com

unread,
Apr 29, 2021, 8:04:48 PM4/29/21
to Ansible Project
Hi!

I would like to skip the unzip of my task if the directory already exists.  Here are my info.

contents of playbook:  install_zip.yml
---
 - name: install applications from a zip
   hosts: all
   roles:
      - install_zip

contents of roles/install_zip/vars/main.yml
pt_dir: 'c:\temp-ansible'
jdk_ver: '1.8.0_131'
jdk_file: "jdk-{{ jdk_ver }}.zip"
zip_info:
 - { basedir: "{{ pt_dir }}", abspath: "{{ pt_dir }}\\{{ jdk_file }}", dstpath: "c:\\java\\{{ jdk_ver }}", srcurl: "{{ jdk_baseurl }}/{{ jdk_ver }}/{{ jdk_file}}" }

contents of roles/install_zip/tasks/main.yml
---
 - name: create base directory
   ansible.windows.win_file:
     path: "{{ item.basedir }}"
     state: directory
   with_items: "{{ zip_info }}"
   when: ( ansible_os_family == "Windows" )

 - name: download zip
   ansible.windows.win_get_url:
     url: "{{ item.srcurl }}"
     dest: "{{ item.abspath }}"
   with_items: "{{ zip_info }}"
   when: ( ansible_os_family == "Windows" )

 - name: check if directory exists
   win_stat:
     path: "{{ item.dstpath }}"
   with_items: "{{ zip_info }}"
   register: dir_stat
   when: ( ansible_os_family == "Windows" )

 - name: print variable
   debug:
     msg: "{{ dir_stat }}"
   when: ( ansible_os_family == "Windows" )

 - name: decompress zip
   win_unzip:
     dest: "{{ item.dstpath }}"
     src: "{{ item.abspath }}"
   with_items: "{{ zip_info }}"
   when: ( ansible_os_family == "Windows" ) 

here is my output 
PLAY [install applications from a zip] ****************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [10.1.1.1]

TASK [install_zip : create base directory] ************************************************************************
ok: [10.1.1.1] => (item={'basedir': 'c:\\temp-ansible', 'abspath': 'c:\\temp-ansible\\jdk-1.8.0_131.zip', 'dstpath': 'c:\\java\\1.8.0_131', 'srcurl': 'https://server1.example.com/jdk/1.8.0_131/jdk-1.8.0_131.zip'})

TASK [install_zip : download zip] *********************************************************************************
ok: [10.1.1.1] => (item={'basedir': 'c:\\temp-ansible', 'abspath': 'c:\\temp-ansible\\jdk-1.8.0_131.zip', 'dstpath': 'c:\\java\\1.8.0_131', 'srcurl': 'https://server1.example.com/jdk/1.8.0_131/jdk-1.8.0_131.zip'})

TASK [install_zip : check if directory exists] ********************************************************************
ok: [10.1.1.1] => (item={'basedir': 'c:\\temp-ansible', 'abspath': 'c:\\temp-ansible\\jdk-1.8.0_131.zip', 'dstpath': 'c:\\java\\1.8.0_131', 'srcurl': 'https://server1.example.com/jdk/1.8.0_131/jdk-1.8.0_131.zip'})

TASK [install_zip : print variable] *******************************************************************************
ok: [10.1.1.1] => 
  msg:
    changed: false
    msg: All items completed
    results:
    - ansible_loop_var: item
      changed: false
      failed: false
      invocation:
        module_args:
          checksum_algorithm: sha1
          follow: false
          get_checksum: true
          path: c:\java\1.8.0_131
      item:
        abspath: c:\temp-ansible\jdk-1.8.0_131.zip
        basedir: c:\temp-ansible
        dstpath: c:\java\1.8.0_131
      stat:
        attributes: Directory
        creationtime: 1619713791.4490106
        exists: true
        filename: 1.8.0_131
        hlnk_targets: []
        isarchive: false
        isdir: true
        ishidden: false
        isjunction: false
        islnk: false
        isreadonly: false
        isreg: false
        isshared: false
        lastaccesstime: 1619713791.5422983
        lastwritetime: 1619713791.5422983
        nlink: 1
        owner: BUILTIN\Administrators
        path: c:\java\1.8.0_131
        size: 372169148
skipping: [Dev0015248]

PLAY RECAP ********************************************************************************************************
10.1.1.1                : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

How do I capture `exists: true`?

Help.

Thank you in advance.

Regards,
j
Message has been deleted

zil...@gmail.com

unread,
Jun 21, 2021, 9:52:26 PM6/21/21
to Ansible Project
Try

- name: decompress zip
   win_unzip:
     dest: "{{ item.dstpath }}"
     src: "{{ item.abspath }}"
   with_items: "{{ zip_info }}"
   when: ( ansible_os_family == "Windows" and not dir_stat.stat.exists )

Chris
Reply all
Reply to author
Forward
0 new messages