gpt4 book ai didi

带通配符的 Ansible 复制文件?

转载 作者:行者123 更新时间:2023-12-03 15:21:12 28 4
gpt4 key购买 nike

在我的文件目录中,我有各种文件,名称结构相似:

data-example.zip
data-precise.zip
data-arbitrary.zip
data-collected.zip

我想使用 Ansible 在远程机器的/tmp 目录中传输所有这些文件,而无需明确指定每个文件名。
换句话说,我想传输每个以“data-”开头的文件。

这样做的正确方法是什么?在类似的帖子中,有人建议使用 with_fileglob关键字, - 但我无法让它发挥作用。
有人可以为我提供一个如何完成上述任务的例子吗?

最佳答案

方法1:找到所有文件,将它们存储在一个变量中,然后将它们复制到目的地。

- hosts: lnx
tasks:
- find: paths="/source/path" recurse=yes patterns="data*"
register: file_to_copy
- copy: src={{ item.path }} dest=/dear/dir
owner: root
mode: 0775
with_items: "{{ files_to_copy.files }}"

使用 remote_src: yes 将远程机器中的文件从一个路径复制到另一个路径。

Ansible documentation

方法二:Fileglob
- name: Copy each file over that matches the given pattern
copy:
src: "{{ item }}"
dest: "/etc/fooapp/"
owner: "root"
mode: 0600
with_fileglob:
- "/playbooks/files/fooapp/*"

Ansible documentation

关于带通配符的 Ansible 复制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62116534/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com