【问题标题】:Create archive file without its structure in ansible在ansible中创建没有其结构的存档文件
【发布时间】:2020-01-13 08:48:40
【问题描述】:

我需要创建一个 tar.gz 存档,其中包含不同目录中的两个文件,但我希望 tar.gz 内部不会出现父结构。我没有第一个文件的名称,但我有它所在的目录。现在我正在使用此代码

- name: Create tar
          archive:
                path:
                - /var/opt/xxx/directory
                - /home/file2
                dest: /root/newDirectory/compressedFile.tar.gz
                format: gz

我希望compressedFile.tar.gz 仅包含目录/var/opt/xxx/directory 和file2 中的file1,但在此代码中显示整个压缩目录结构

没有父结构怎么办?

【问题讨论】:

  • 是否可以将这两个文件复制到一个目录然后压缩?
  • 有可能,这种情况会怎样?我一直在寻找最高效、最优雅的方式

标签: directory ansible parent tar archive


【解决方案1】:

这是一个可能的场景(待测试/调整)。

- name: Directory on local machine to hold relevant files
  file:
    state: directory
    path: "/tmp/my_archive_{{ inventory_hostname_short }}"
  deletegate_to: localhost

- name: Copy relevant files to localhost
  fetch:
    dest: "/tmp/my_archive_{{ inventory_hostname_short }}/"
    src: "{{ item }}"
    flat: true
  loop:
    - /var/opt/xxx/directory
    - /home/file2

- name: Create the archive
  archive:
    dest: "/tmp/my_archive_{{ inventory_hostname_short }}/archive.tar.gz"
    path: "/tmp/my_archive_{{ inventory_hostname_short }}/*"
  delegate_to: localhost

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 2020-04-30
    • 2018-11-20
    • 2018-09-29
    • 1970-01-01
    • 2011-11-05
    • 2023-04-11
    相关资源
    最近更新 更多