是否可以使用通配符复制文件?

问题描述 投票:0回答:2

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

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

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

正确的方法是什么?在类似的主题中,有人建议使用with_fileglob关键字-但我无法使它正常工作。有人可以为我提供有关如何完成上述任务的示例吗?

ansible copy wildcard provisioning
2个回答
1
投票

方法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 }}"

方法2: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/*"

参考:https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html


0
投票

在发布问题后不久,我实际上自己想出了问题。 with_fileglob关键字是实现此目的的方法。

- name: "Transferring all data files"
  copy:
    src: "{{ item }}"
    dest: /tmp/
  with_fileglob: "data-*"
© www.soinside.com 2019 - 2024. All rights reserved.