community.general.filetree 查找 – 递归匹配目录树中的所有文件
注意
此查找插件是 community.general 集合(版本 10.1.0)的一部分。
如果您正在使用 ansible 包,您可能已经安装了这个集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list。
要安装它,请使用:ansible-galaxy collection install community.general。
要在 playbook 中使用它,请指定:community.general.filetree。
概要
- 此查找使您能够在目标系统上模板化完整的文件树,同时保留权限和所有权。 
- 支持目录、文件和符号链接,包括 SELinux 和其他文件属性。 
- 如果您提供多个路径,它将实现 first_found 逻辑,并且不会处理它在先前路径中已处理的条目。这使得可以按重要性顺序合并不同的树,或向特定路径添加 role_vars 以影响同一角色的不同实例。 
术语
| 参数 | 注释 | 
|---|---|
| 要读取的文件的路径。 | 
示例
- name: Create directories
  ansible.builtin.file:
    path: /web/{{ item.path }}
    state: directory
    mode: '{{ item.mode }}'
  with_community.general.filetree: web/
  when: item.state == 'directory'
- name: Template files (explicitly skip directories in order to use the 'src' attribute)
  ansible.builtin.template:
    src: '{{ item.src }}'
    # Your template files should be stored with a .j2 file extension,
    # but should not be deployed with it. splitext|first removes it.
    dest: /web/{{ item.path | splitext | first }}
    mode: '{{ item.mode }}'
  with_community.general.filetree: web/
  when: item.state == 'file'
- name: Recreate symlinks
  ansible.builtin.file:
    src: '{{ item.src }}'
    dest: /web/{{ item.path }}
    state: link
    follow: false  # avoid corrupting target files if the link already exists
    force: true
    mode: '{{ item.mode }}'
  with_community.general.filetree: web/
  when: item.state == 'link'
- name: list all files under web/
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.filetree', 'web/') }}"
返回值
| 键 | 描述 | 
|---|---|
| 包含文件信息的字典列表。 返回: 成功 | |
| 上次元数据更新或创建的时间(取决于操作系统)。 返回: 成功 | |
| 文件/目录的组 ID。 返回: 成功 | |
| 拥有文件/目录的组的名称。 返回: 成功 | |
| 生成的文件或目录的权限。 返回: 成功 | |
| 上次修改的时间。 返回: 成功 | |
| 拥有文件/目录的用户的名称。 返回: 成功 | |
| 包含到根目录的相对路径。 返回: 成功 | |
| 允许按原始位置进行筛选。 返回: 成功 | |
| SELinux 文件上下文的级别部分。 返回: 成功 | |
| SELinux 文件上下文的角色部分。 返回: 成功 | |
| SELinux 文件上下文的类型部分。 返回: 成功 | |
| SELinux 文件上下文的用户部分。 返回: 成功 | |
| 目标的大小。 返回: 成功 | |
| 待办事项 返回: 成功 | |
| 文件/目录的所有者 ID。 返回: 成功 | 
