Ansible 使用列表中的存档模块创建 tar.gz [英] Ansible create tar.gz with archive module from a list
问题描述
我试图在 Ansible 中查找早于一天的文件,然后创建这些文件的 tar.gz,我尝试了 archive
模块,尽管它只创建了一个 tar列表中的最后一个元素.有没有办法创建包含所有文件的 tar.gz?
I'm trying to find files older than one day in Ansible and after that, create a tar.gz of those files, I've tried archive
module, although it is creating only a tar of the last element in the list. Is there any way to create tar.gz including all files?
下面是我的脚本:
- name: Check all files
find: paths=/myfiles
file_type=file
age=1
age_stamp=mtime
register: files
failed_when: files.matched < 10
- name: Remove previous tarFile
file: path=/tmp/test.tar.gz
state=absent
- name: compress all the files in tar.gz
archive: path="{{ item.path }}"
dest=/tmp/test.tar.gz
format=gz
with_items:
"{{ files.files }}"
推荐答案
它只创建列表中最后一个元素的 tar
it is creating only a tar of the last element in the list
它正在为循环中的每个文件创建和覆盖 /tmp/test.tar.gz
.当您检查时,您只会看到最后一个.
It is creating and overwriting /tmp/test.tar.gz
for each file in the loop. When you check, you see only the last one.
如果您查看 archive
模块文档,你会看到:
If you look at the archive
module docs, you will see:
path
远程绝对路径、glob 或 路径列表 或要压缩或存档的文件的 glob.
path
Remote absolute path, glob, or list of paths or globs for the file or files to compress or archive.
因此您可以提供文件列表作为 path
参数的值:
So you can provide the list of files as a value for the path
parameter:
- name: compress all the files in tar.gz
archive:
path: "{{ files_to_archive }}"
dest: /tmp/test.tar.gz
format: gz
vars:
files_to_archive: "{{ files.files | map(attribute='path') | list }}"
这篇关于Ansible 使用列表中的存档模块创建 tar.gz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!