# Unarchive - using wildcards in 'src' option

**URL:** <https://forum.ansible.com/t/unarchive-using-wildcards-in-src-option/4876>\
**Category:** Get Help\
**Created:** [April 10, 2024, 8:54pm UTC](https://forum.ansible.com/t/unarchive-using-wildcards-in-src-option/4876 "2024-04-10T20:54:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [April 10, 2024, 8:54pm UTC](https://forum.ansible.com/t/unarchive-using-wildcards-in-src-option/4876/1 "2024-04-10T20:54:06Z")

</div>

I have these 2 tasks wich should download the latest a tar archive and then unpack it

```yaml
    - name: get nexus 
      ansible.builtin.get_url: 
        url: https://download.sonatype.com/nexus/3/latest-unix.tar.gz
        dest: /tmp/
        owner: "{{ app_name }}"
        group: "{{ app_name }}"

    - name: unpack nexus.tar
      ansible.builtin.unarchive:
        src: /tmp/nexus-3.67.0-03-unix.tar.gz
        dest: "/opt/"
        owner: "{{ app_name }}"
        group: "{{ app_name }}"
        remote_src: true
        mode: 0700

```

I go via a link **`https://download.sonatype.com/nexus/3/latest-unix.tar.gz`** whitch results into downloading the specific **`nexus-3.67.0-03-unix.tar.gz`**

I do not know the exact version upfront so I am want to mimic unarchiving similar to  
**`tar -xvf /opt/nexus-3*.tar.gz`** but can not figure out a way to apply any sort of \* logic in the `src` option of the `unarchive` TASK though.

Is that doable?

I tried to make this work via `stat`, but that neither would allow \* in its `path` option.

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [April 10, 2024, 9:08pm UTC](https://forum.ansible.com/t/unarchive-using-wildcards-in-src-option/4876/2 "2024-04-10T21:08:07Z")

</div>

I’m unable to test since [download.sonatype.com](http://download.sonatype.com) is blocked by my company firewall, but I think you can just register the `get_url:` task to discover what the resulting filename was. Then unarchive the variable name.

Something like:

```yaml
- name: get nexus 
      ansible.builtin.get_url: 
        url: https://download.sonatype.com/nexus/3/latest-unix.tar.gz
        dest: /tmp/
        owner: "{{ app_name }}"
        group: "{{ app_name }}"
      register: nexus

    - name: unpack nexus.tar
      ansible.builtin.unarchive:
        src: "{{ nexus.dest }}"
        dest: "/opt/"
        owner: "{{ app_name }}"
        group: "{{ app_name }}"
        remote_src: true
        mode: 0700

```

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [April 10, 2024, 10:31pm UTC](https://forum.ansible.com/t/unarchive-using-wildcards-in-src-option/4876/3 "2024-04-10T22:31:27Z")

</div>

Another option is to specify a destination filename instead of a directory:

```yaml
    - name: get nexus 
      ansible.builtin.get_url: 
        url: https://download.sonatype.com/nexus/3/latest-unix.tar.gz
        dest: /tmp/nexus-latest-unix.tar.gz
        owner: "{{ app_name }}"
        group: "{{ app_name }}"

```

There’s no particular reason to respect the server-provided filename in this case.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 11, 2024, 9:49am UTC](https://forum.ansible.com/t/unarchive-using-wildcards-in-src-option/4876/4 "2024-04-11T09:49:30Z")

</div>

Another approach would be to use the URI module to do a HEAD request to get the `Location` of the file and then get the filename from that, for example (untested):

```yaml
- name: Check the latest version of Nexus 3
  ansible.builtin.uri:
    url: https://download.sonatype.com/nexus/3/latest-unix.tar.gz
    method: HEAD
    status_code: 302 
    follow_redirects: none
  check_mode: false
  changed_when: false
  register: nexus_headers

- name: Debug Nexus 3 latest headers
  ansible.builtin.debug:
    msg:
      - "Location: {{ nexus_headers.location }}"
      - "Path: {{ nexus_headers.location | ansible.builtin.urlsplit('path') }}"
      - "File: {{ nexus_headers.location | ansible.builtin.urlsplit('path') | ansible.builtin.basename }}"
      - "Version: {{ nexus_headers.location | ansible.builtin.urlsplit('path') | ansible.builtin.basename | ansible.builtin.regex_replace('^nexus-') | ansible.builtin.regex_replace('[.]tar[.]gz$') }}"

```

This approach might make sense if you want to only download an update if the specific version isn’t already installed. 🤷

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 11, 2024, 9:49am UTC](https://forum.ansible.com/t/unarchive-using-wildcards-in-src-option/4876/5 "2024-05-11T09:49:48Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
