26

When extracting a tar.gz file in ansible I end up with a first directory

- name: Extract archive
  unarchive: src=file.tar.gz
             dest=/foo/bar

which results in /foo/bar/bar-version-someFirstLevelFolder/contentOfArchive How can I prevent creating this extra level of hierarchy?

  • May not be relevant but I know you sometimes need to be careful in adding a trailing slash to the destination directory. Does that help at all? – SauceCode Feb 20 '17 at 18:35
37

In order to strip the bar-version-someFirstLevelFolder you need to use the --strip-components=1 option in tar. So your playbook should look like

- name: Extract archive
  unarchive:
    src: file.tar.gz
    dest: /foo/bar
    extra_opts: [--strip-components=1]
  • 1
    This seems to be the correct answer, but currently it can cause a crash. See this bug report: github.com/ansible/ansible/issues/29657 an intermediate solution would be to untar in the parent directory and then have a command: mv /foo/bar-version-someFirstLevelFolder/* foo/bar/ – TheAtomicOption Nov 28 '17 at 1:22
  • Tested in the last version, it works. But, unarchive is not idempotent. – Jérôme B Aug 13 '18 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.