Ansible. Get a directory name out of "tar.gz" archive without decompressing

758 Views Asked by At

I am installing nexus using Ansible. Ansible task downloads the last version packed as "tar.gz" archive and unpacks it into the target directory. The archive contains two directories. For example:

/srv
  |- /nexus-3.34.0-01
  |- /sonatype-work

Then Ansible need to create a symbolic link. Like this:

/srv
  |- /nexus-3.34.0-01
  |- /sonatype-work
  |- ~nexus -> /srv/nexus-3.34.0-01

Ansible don't know the name of the nexus directory. It is not a problem, if we have only one of them:

- name : Find the name of the nexus directory
  ansible.builtin.find:
    paths: /srv
    patterns: 'nexus-*'
    file_type: directory
  register: nexus_dir

- name: Create a symbolic link for Nexus
  ansible.builtin.file:
    src: "{{ nexus_dir.files[0].path }}"
    dest: /srv/nexus
    state: link   

THE PROBLEM IS: The directory srv already contains previous nexus installations. How can I look into the original archive to find the name of the current nexus directory?

1

There are 1 best solutions below

0
30thh On BEST ANSWER

Here is the solution I found:

- name : Find the name of the nexus directory
  shell: tar --gzip --list  --file={{ filename_nexus }} | grep -o -E -m 1 ^nexus-[^/]+
  register: nexus_dir

- name: Print found nexus installation
  ansible.builtin.debug:
    msg: "Following nexus installation found: {{ nexus_dir.stdout }}"

where grep -o -E -m 1 returns the first match of the provided regular expression.