没有第一个目录的ansible提取

Geo*_*ler 42 tar ansible

在ansible中提取tar.gz文件时,我最终得到了第一个目录

- name: Extract archive
  unarchive: src=file.tar.gz
             dest=/foo/bar
Run Code Online (Sandbox Code Playgroud)

这导致/foo/bar/bar-version-someFirstLevelFolder/contentOfArchive 如何防止创建这种额外的层次结构?

GMa*_*ter 59

为了剥离bar-version-someFirstLevelFolder您需要使用--strip-components=1的选项tar。所以你的剧本应该看起来像

- name: Extract archive
  unarchive:
    src: file.tar.gz
    dest: /foo/bar
    extra_opts: [--strip-components=1]
Run Code Online (Sandbox Code Playgroud)

  • 这似乎是正确的答案,但目前它可能会导致崩溃。请参阅此错误报告:http://github.com/ansible/ansible/issues/29657 中间解决方案是在父目录中解压缩,然后使用 `command: mv /foo/bar-version-someFirstLevelFolder/* foo /酒吧/` (2认同)
  • 如果您只想提取文件的子集(例如“tar -xzf file.tar.gz dirname_in_tar/file.xyz”),您也可以将文件添加到“extra_opts”,但需要添加他们**未剥皮**。示例:“extra_opts: [ --strip-components=1, dirname_in_tar/file.xyz ]”只会从“dirname_in_tar”目录中提取“file.xyz”。 (2认同)