Search code examples
ansible

Compress directory with another name


I have this task:

- archive:
    path: ./my_dir
    dest: ./dir.zip
    format: zip

Resulting dir.zip contains a directory my_dir/. Is it possible to rename the directory when compressed so it's called "whatever/" instead of "my_dir/"?

I can't rename the original directory. I want to rename it just when it's being compressed.


Solution

  • Is it possible to rename the directory when compressed?

    No, such is not implemented. But according the documentation Examples, a minimal example playbook

    ---
    - hosts: localhost
      become: false
      gather_facts: false
    
      tasks:
    
      - archive:
          path: test/*
          dest: test.zip
          format: zip
    

    will result into a ZIP files with content of

    unzip -t test.zip
    Archive:  test.zip
        testing: folder/one               OK
        testing: folder/two               OK
        testing: folder/three             OK
        testing: one                      OK
        testing: three                    OK
        testing: two                      OK
    No errors detected in compressed data of test.zip.
    

    Means, creating an archive of a globbed path will leave out the unwanted directory name and provide the opportunity to specify an other directory during extraction.