ansible.builtin.find (v2.3.2.0-1) — module

return a list of files based on specific criteria

| "added in version" 2.0 of ansible.builtin"

Authors: Brian Coca (based on Ruggero Marchei's Tidy)

stableinterface | supported by core

Install Ansible via pip

Install with pip install ansible==2.3.2.0.post1

Description

Return a list of files based on specific criteria. Multiple criteria are AND'd together.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Recursively find /tmp files older than 2 days
- find:
    paths: "/tmp"
    age: "2d"
    recurse: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Recursively find /tmp files older than 4 weeks and equal or greater than 1 megabyte
- find:
    paths: "/tmp"
    age: "4w"
    size: "1m"
    recurse: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Recursively find /var/tmp files with last access time greater than 3600 seconds
- find:
    paths: "/var/tmp"
    age: "3600"
    age_stamp: atime
    recurse: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# find /var/log files equal or greater than 10 megabytes ending with .old or .log.gz
- find:
    paths: "/var/tmp"
    patterns: "*.old,*.log.gz"
    size: "10m"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# find /var/log files equal or greater than 10 megabytes ending with .old or .log.gz via regex
# Note that yaml double quotes require escaping backslashes but yaml single
# quotes do not.
- find:
    paths: "/var/tmp"
    patterns: "^.*?\\.(?:old|log\\.gz)$"
    size: "10m"
    use_regex: True

Inputs

    
age:
    default: null
    description:
    - Select files whose age is equal to or greater than the specified time. Use a negative
      age to find files equal to or less than the specified time. You can choose seconds,
      minutes, hours, days, or weeks by specifying the first letter of any of those words
      (e.g., "1w").
    required: false

size:
    default: null
    description:
    - Select files whose size is equal to or greater than the specified size. Use a negative
      size to find files equal to or less than the specified size. Unqualified values
      are in bytes, but b, k, m, g, and t can be appended to specify bytes, kilobytes,
      megabytes, gigabytes, and terabytes, respectively. Size is not evaluated for directories.
    required: false

paths:
    aliases:
    - name
    - path
    description:
    - List of paths of directories to search. All paths must be fully qualified.
    required: true

follow:
    choices:
    - true
    - false
    default: 'False'
    description:
    - Set this to true to follow symlinks in path for systems with python 2.6+
    required: false

hidden:
    choices:
    - true
    - false
    default: 'False'
    description:
    - Set this to true to include hidden files, otherwise they'll be ignored.
    required: false

recurse:
    choices:
    - 'yes'
    - 'no'
    default: 'no'
    description:
    - If target is a directory, recursively descend into the directory looking for files.
    required: false

contains:
    default: null
    description:
    - One or more regex patterns which should be matched against the file content
    required: false

patterns:
    aliases:
    - pattern
    default: '*'
    description:
    - One or more (shell or regex) patterns, which type is controlled by C(use_regex)
      option.
    - The patterns restrict the list of files to be returned to those whose basenames
      match at least one of the patterns specified. Multiple patterns can be specified
      using a list.
    required: false

age_stamp:
    choices:
    - atime
    - mtime
    - ctime
    default: mtime
    description:
    - Choose the file property against which we compare age. Default is mtime.
    required: false

file_type:
    choices:
    - file
    - directory
    - link
    - any
    default: file
    description:
    - Type of file to select
    - The 'link' and 'any' choices were added in version 2.3
    required: false

use_regex:
    choices:
    - true
    - false
    default: 'False'
    description:
    - If false the patterns are file globs (shell) if true they are python regexes
    required: false

get_checksum:
    choices:
    - true
    - false
    default: 'False'
    description:
    - Set this to true to retrieve a file's sha1 checksum
    required: false

Outputs

examined:
  description: number of filesystem objects looked at
  returned: success
  sample: 34
  type: string
files:
  description: all matches found with the specified criteria (see stat module for
    full output of each dictionary)
  returned: success
  sample:
  - '...': '...'
    checksum: 16fac7be61a6e4591a33ef4b729c5c3302307523
    mode: '0644'
    path: /var/tmp/test1
  - '...': '...'
    path: /var/tmp/test2
  type: list of dictionaries
matched:
  description: number of matches
  returned: success
  sample: 14
  type: string