Ansible:从本地目录获取文件列表

Kiv*_*iva 14 ansible

我使用ansible 1.9.4,我想从本地目录中获取文件列表.

在2.0版本中,有find模块,但此版本是beta版.

如何在<2.0中执行此操作?

Ber*_*ale 16

前段时间我正在构建一个需要这样的东西的自动化 - 请参阅Ansible发送文件到第一个目的地.

在ansible 2.0之前,没有使用command或者没有办法做到这一点 shell.

如果你真的无法升级到ansible 2.0,请使用该command模块:

vars:
  directory: /path/to/dir

tasks:

  - command: "ls {{directory}}"
    register: dir_out

  - debug: var={{item}}
    with_items: dir_out.stdout_lines
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。我考虑过这个模块,但我发现它很难看。但如果这是唯一的解决方案,那就走吧。 (2认同)

Pet*_*art 14

现在有一个可以使用的查找模块。示例:显示名称以“ansible”开头且位置为 /tmp 的文件夹,因此 /tmp/ansible*

- name: ls -d /tmp/ansible*
  find:
    paths: /tmp
    patterns: "ansible*"
    recurse: no
    file_type: directory
  register: found_directories

- debug:
    msg: "{{ [item.path] }} "
  with_items: "{{ found_directories.files }}"
Run Code Online (Sandbox Code Playgroud)

  • 根据我所做的测试,“find”适用于远程,而不适用于本地。 (2认同)

小智 9

这是在目录模板中列出扩展名为.j2的所有文件并将它们传递给模块的示例.

template: src="{{ item }}" dest="generated/{{ inventory_hostname }}/{{ item | basename | replace('.j2', '')}}"
  delegate_to: 127.0.0.1
  with_fileglob: templates/*.j2
Run Code Online (Sandbox Code Playgroud)