Alex
Alex

Reputation: 17

Create archive file without its structure in ansible

I need create a tar.gz archive with two files in differents directories but i want that inside the tar.gz not appear the parent structure. I don't have the name of the first file but I do have the directory in which it is located. Now i'm use this code

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

I want that compressedFile.tar.gz contain only file1 that is inside the directory /var/opt/xxx/directory and file2 but in this momment with this code the entire compressed directory structure appears

How can I do not to have the parent structure?

Upvotes: 0

Views: 1060

Answers (1)

Zeitounator
Zeitounator

Reputation: 44645

This is a possible scenario (to be tested/adapted).

- 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

Upvotes: 1

Related Questions