ansible.windows.win_find 模块 – 根据特定条件返回文件列表

注意

此模块是 ansible.windows 集合(版本 2.5.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了这个集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install ansible.windows

要在 playbook 中使用它,请指定:ansible.windows.win_find

概要

  • 根据指定的条件返回文件列表。

  • 多个条件之间是“与”的关系。

  • 对于非 Windows 目标,请改用 ansible.builtin.find 模块。

参数

参数

注释

age

字符串

选择年龄等于或大于指定时间的文件或文件夹。

使用负年龄查找等于或小于指定时间的文件。

您可以通过指定这些单词的首字母来选择秒、分钟、小时、天或周(例如,“2s”、“10d”、1w”)。

age_stamp

字符串

选择我们用于比较 age 的文件属性。

默认情况下,我们比较的属性是上次修改时间。

选择

  • "atime"

  • "ctime"

  • "mtime" ←(默认)

checksum_algorithm

字符串

确定文件校验和的算法。

如果主机无法使用指定的算法,则会抛出错误。

选择

  • "md5"

  • "sha1" ←(默认)

  • "sha256"

  • "sha384"

  • "sha512"

depth

整数

在 ansible.windows 2.0.0 中添加

设置要下降的最大级别数。

将 recurse 设置为 false 将覆盖此值,这实际上是深度 1。

默认深度是不受限制的。

file_type

字符串

要搜索的文件类型。

选择

  • "directory"

  • "file" ←(默认)

follow

布尔值

将其设置为 true 以跟踪路径中的符号链接。

这需要与 recurse 结合使用。

选择

  • false ←(默认)

  • true

get_checksum

布尔值

是否在返回信息中返回文件的校验和(默认 sha1),使用 checksum_algorithm 从默认值更改。

选择

  • false

  • true ←(默认)

hidden

布尔值

将其设置为包括隐藏的文件或文件夹。

选择

  • false ←(默认)

  • true

paths

列表 / 元素=字符串 / 必需

要在其中搜索文件或文件夹的目录路径列表。

这可以作为单个路径或路径列表提供。

patterns

别名:regex, regexp

列表 / 元素=字符串

一个或多个(powershell 或 regex)模式,用于比较文件名。

模式匹配的类型由 use_regex 选项控制。

模式根据文件名限制要返回的文件或文件夹列表。

要匹配文件,它只需要与提供的列表中的一个模式匹配。

recurse

布尔值

将递归下降到目录中查找文件或文件夹。

选择

  • false ←(默认)

  • true

size

字符串

选择大小等于或大于指定大小的文件或文件夹。

使用负值查找等于或小于指定大小的文件。

您可以使用字节类型后缀指定大小,即 kilo = k,mega = m...

不会评估符号链接的大小。

use_regex

布尔值

如果设置为 true,则会将模式设置为作为正则表达式检查运行。

选择

  • false ←(默认)

  • true

说明

注意

  • 扫描包含大量数据的具有大量文件的目录时,建议设置 get_checksum=false。这将加快扫描文件夹所需的时间,因为获取校验和需要读取返回的每个文件的内容。

示例

- name: Find files in path
  ansible.windows.win_find:
    paths: D:\Temp

- name: Find hidden files in path
  ansible.windows.win_find:
    paths: D:\Temp
    hidden: true

- name: Find files in multiple paths
  ansible.windows.win_find:
    paths:
      - C:\Temp
      - D:\Temp

- name: Find files in directory while searching recursively
  ansible.windows.win_find:
    paths: D:\Temp
    recurse: true

- name: Find files in directory while following symlinks
  ansible.windows.win_find:
    paths: D:\Temp
    recurse: true
    follow: true

- name: Find files with .log and .out extension using powershell wildcards
  ansible.windows.win_find:
    paths: D:\Temp
    patterns: ['*.log', '*.out']

- name: Find files in path based on regex pattern
  ansible.windows.win_find:
    paths: D:\Temp
    patterns: out_\d{8}-\d{6}.log

- name: Find files older than 1 day
  ansible.windows.win_find:
    paths: D:\Temp
    age: 86400

- name: Find files older than 1 day based on create time
  ansible.windows.win_find:
    paths: D:\Temp
    age: 86400
    age_stamp: ctime

- name: Find files older than 1 day with unit syntax
  ansible.windows.win_find:
    paths: D:\Temp
    age: 1d

- name: Find files newer than 1 hour
  ansible.windows.win_find:
    paths: D:\Temp
    age: -3600

- name: Find files newer than 1 hour with unit syntax
  ansible.windows.win_find:
    paths: D:\Temp
    age: -1h

- name: Find files larger than 1MB
  ansible.windows.win_find:
    paths: D:\Temp
    size: 1048576

- name: Find files larger than 1GB with unit syntax
  ansible.windows.win_find:
    paths: D:\Temp
    size: 1g

- name: Find files smaller than 1MB
  ansible.windows.win_find:
    paths: D:\Temp
    size: -1048576

- name: Find files smaller than 1GB with unit syntax
  ansible.windows.win_find:
    paths: D:\Temp
    size: -1g

- name: Find folders/symlinks in multiple paths
  ansible.windows.win_find:
    paths:
      - C:\Temp
      - D:\Temp
    file_type: directory

- name: Find files and return SHA256 checksum of files found
  ansible.windows.win_find:
    paths: C:\Temp
    get_checksum: true
    checksum_algorithm: sha256

- name: Find files and do not return the checksum
  ansible.windows.win_find:
    paths: C:\Temp
    get_checksum: false

返回值

常见的返回值记录在此处,以下是此模块独有的字段

描述

examined

整数

检查的文件/文件夹数量。

返回值: 始终

示例: 10

files

复杂

与条件匹配的文件/文件夹的信息,作为每个匹配文件的字典元素列表返回。这些条目按路径值按字母顺序排序。

返回值: 成功

attributes

字符串

路径处文件的原始属性形式。

返回值: 成功,路径存在

示例: "Archive, Hidden"

checksum

字符串

基于指定的 checksum_algorithm 的文件校验和。

返回值: 成功,路径存在,路径是文件,get_checksum == True

示例: "09cb79e8fc7453c84a07f644e441fd81623b7f98"

creationtime

浮点数

文件创建时间,以自 epoch 以来的秒数表示。

返回值: 成功,路径存在

示例: 1477984205.15

exists

布尔值

文件是否存在,对于 ansible.windows.win_find 始终为 true。

返回值: 成功,路径存在

示例: true

extension

字符串

文件路径的扩展名。

返回: 成功,路径存在,路径是文件

示例: ".ps1"

filename

字符串

文件名。

返回值: 成功,路径存在

示例: "temp"

hlnk_targets

列表 / 元素=字符串

指向同一文件的其他文件列表(硬链接),不包括当前文件。

返回值: 成功,路径存在

示例: ["C:\\temp\\file.txt", "C:\\Windows\\update.log"]

isarchive

布尔值

路径是否已准备好进行存档。

返回值: 成功,路径存在

示例: true

isdir

布尔值

路径是否为目录。

返回值: 成功,路径存在

示例: true

ishidden

布尔值

路径是否隐藏。

返回值: 成功,路径存在

示例: true

isjunction

布尔值

路径是否为连接点。

返回值: 成功,路径存在

示例: true

islnk

布尔值

路径是否为符号链接。

返回值: 成功,路径存在

示例: true

isreadonly

布尔值

路径是否为只读。

返回值: 成功,路径存在

示例: true

isreg

布尔值

路径是否为常规文件。

返回值: 成功,路径存在

示例: true

isshared

布尔值

路径是否为共享。

返回值: 成功,路径存在

示例: true

lastaccesstime

浮点数

文件最后访问时间,以自 epoch 以来的秒数表示。

返回值: 成功,路径存在

示例: 1477984205.15

lastwritetime

浮点数

文件最后修改时间,以自 epoch 以来的秒数表示。

返回值: 成功,路径存在

示例: 1477984205.15

lnk_source

字符串

为远程文件系统规范化的符号链接的目标。

返回: 成功,路径存在,路径是符号链接或连接点

示例: "C:\\temp"

lnk_target

字符串

符号链接的目标。请注意,相对路径保持相对,如果不是链接,则返回 null。

返回: 成功,路径存在,路径是符号链接或连接点

示例: "temp"

整数

指向该文件的链接数(硬链接)

返回值: 成功,路径存在

示例: 1

owner

字符串

文件的所有者。

返回值: 成功,路径存在

示例: "BUILTIN\\Administrators"

path

字符串

文件的完整绝对路径。

返回值: 成功,路径存在

示例: "C:\\temp\\file.txt"

sharename

字符串

如果文件夹已共享,则为共享名称。

返回: 成功,路径存在,路径是目录且 isshared == True

示例: "file-share"

size

整数

文件的大小(以字节为单位)。

返回: 成功,路径存在,路径是文件

示例: 1024

matched

整数

符合条件的文件/文件夹数量。

返回值: 始终

示例: 2

作者

  • Jordan Borean (@jborean93)