Ansible循环遍历文件

Ansible looping through files

在 Ansible 2.5 之前,循环的语法曾经是 with_x。从 2.5 开始,loop 受到青睐,with_x 基本上从文档中消失了。

不过,文档中提到了如何将 with_x 替换为 loop 的示例。但是我对我们现在应该如何遍历文件目录一无所知。

假设我需要上传给定目录中的所有文件,我曾经使用 with_fileglob

- name: Install local checks
  copy:
    src: "{{ item }}"
    dest: /etc/sensu/plugins/
    owner: sensu
    group: sensu
    mode: 0744
  with_fileglob:
    - plugins/*

那么现代的对应物是什么?有可能吗?我知道我仍然可以使用 with_fileglob 但在我编写新角色时,我最好让它们面向未来。

相当于

    loop: "{{ lookup('fileglob', 'plugins/*', wantlist=True) }}"

这里是 doc.

从当前Ansible loops doc开始:

Any with_* statement that requires using lookup within a loop should not be converted to use the loop keyword. For example, instead of doing:

loop: "{{ lookup('fileglob', '*.txt', wantlist=True) }}"

it’s cleaner to keep:

with_fileglob: '*.txt'