【问题标题】:Ansible. Get a directory name out of "tar.gz" archive without decompressing安西布尔。从“tar.gz”存档中获取目录名称而不解压缩
【发布时间】:2021-11-01 20:51:10
【问题描述】:

我正在使用 Ansible 安装 nexus。 Ansible 任务下载打包为“tar.gz”存档的最后一个版本并将其解压缩到目标目录中。存档包含两个目录。例如:

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

那么 Ansible 需要创建一个符号链接。像这样:

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

Ansible 不知道 nexus 目录的名称。如果我们只有其中一个,这不是问题:

- 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   

问题是:srv 目录已经包含以前的 nexus 安装。如何查看原始存档以找到当前 nexus 目录的名称?

【问题讨论】:

  • 你可以查找最近 mtime 的 nexus-* 目录
  • guido 提供了一个非常好的建议,使用new_names | difference(old_names) 也可以工作
  • 在这种非常精确的情况下,调用提供--/latest-unix.tar.gz 的uri 将返回指向.../nexus-x.y.z-unix.tar.gz 的重定向链接。您可以简单地依赖该版本,而无需打开存档。一个例子here
  • 我不能使用“最后一个”目录。我很可能想回滚到以前的版本。我不能使用“差异”。很有可能该目录在上次运行脚本时已经创建。

标签: ansible tar.gz


【解决方案1】:

这是我找到的解决方案:

- 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 }}"

其中grep -o -E -m 1 返回提供的正则表达式的第一个匹配项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    相关资源
    最近更新 更多