如何使用 Ansible 复制与 glob 模式匹配的文件

How to copy files that match a glob pattern with Ansible

我有以下本地文件集:

$ find . -maxdepth 2
.
./.DS_Store
./radio
./radio/server.properties
./radio/recordings/
./radio/requirements.txt
./radio/__pycache__
./radio/radio.iml
./radio/radio.py
./radio/audio.py
./ansibleplaybook.yml
./inventory

我只想复制 .py 个文件,这样我的远程服务器看起来像这样:

$ find ~/radio/
/home/pi/radio/
/home/pi/radio/radio.py
/home/pi/radio/audio.py

我使用此处和 Ansible 文档中的示例来编写此剧本:

---
- hosts: all
  tasks:
    - name: Copy radio source
      copy:
        src: "radio"
        dest: "~/radio"
      with_fileglob:
        - "*.py"

但是,运行我运行没有创建目录,也没有复制任何内容。

$ ansible-playbook -i inventory ansibleplaybook.yml -u pi

PLAY [pi] **************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [pi]

TASK [Copy radio source] **********************************************************************************************************************************************

PLAY RECAP ************************************************************************************************************************************************************
pi              : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

如果我单独指定 .py 文件,则会创建无线电目录并复制 .py 文件。我知道我现在只有两个 .py 文件,但每次添加新的 python 模块时都必须更新我的剧本显然会开始变得非常乏味。我怎样才能让上面的剧本起作用,以便复制所有匹配模式的文件?

我认为你的部分困惑是 with_fileglobcopy 模块的参数一无所知,所以它不知道你想要 [=15] 相关的文件=] 目录。例如,如果您从您的主目录 运行 这个 shell 命令会发生什么:

ls *.py

它也找不到那些 *.py 文件,因为它们在 radio 目录。

因为with_fileglobreturns没有结果,任务永远运行s.

有多种方法可以解决这个问题。请注意,在以下示例中,我使用了目标 /tmp/target 以避免将我的主目录与测试文件混在一起。

递归复制

如果删除with_fileglob循环,那么Ansible会递归地将本地radio目录的内容复制到目标主机上的~/radio。这将复制 所有 文件,而不仅仅是 Python 源文件。

使用find模块生成文件列表

如果你真的想要 Python 来源,你可以使用 find 模块生成匹配文件列表,然后使用它作为 copy 模块的输入。在这种情况下,您需要自己创建目标目录,因为 copy 不会为您做这些。

像这样:

- hosts: localhost
  gather_facts: false
  tasks:
    - find:
        paths: radio
        patterns: "*.py"
      register: python_files

    - name: Create target directories
      file:
        path: "/tmp/target/{{ item.path|dirname }}"
        state: directory
      loop: "{{ python_files.files }}"
      loop_control:
        label: "{{ item.path }}"

    - name: Copy files
      copy:
        src: "{{ item.path }}"
        dest: "/tmp/target/{{ item.path }}"
      loop: "{{ python_files.files }}"
      loop_control:
        label: "{{ item.path }}"

使用synchronize模块

您可以使用 synchronize 模块来复制文件。 synchronizersync 的包装器,因此在许多方面它更适合手头的任务。这可能看起来像这样:

- hosts: localhost
  gather_facts: false
  tasks:
    - synchronize:
        src: radio/
        dest: /tmp/target/
        rsync_opts:
          - '--include=*.py'
          - '--include=*/'
          - '--exclude=*'

有关 --include--exclude 选项的更多信息,请参阅 rsync 手册页。

其实你需要的是这个:

---
- hosts: all
  tasks:
  - name: Copy radio source
    copy:
      src: "{{ item }}"
      dest: "~/radio/"
    with_fileglob:
      - "radio/*.py"

注意 dest 上的尾部斜杠“/”以确保写入目录。 正如 larsks 所说,“with”部分对要复制的参数一无所知。实际上 with_fileglob 用于创建要复制的参数,因此 "{{ item }}" 作为引用“with”部分提供的项目的 src,您几乎可以将其视为任务的循环.

参考:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fileglob_lookup.html