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

- name: Extract archive
  unarchive: src=file.tar.gz

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

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
    src: file.tar.gz
    dest: /foo/bar
    extra_opts: [--strip-components=1]
| improve this answer | |
  • 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
  • 1
    Tested in the last version, it works. But, unarchive is not idempotent. – Jérôme B Aug 13 '18 at 21:15
  • If you know content of your archive and you can risk a bit, you can use creates: /some/file/that/unpacks to get some very limited impotence (tried and ansible-2.9.7-1.fc30.noarch correctly skips the task if I rerun the playbook). – jhutar Jun 2 at 21:01

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.