Ansible find Module — Search for Files and Directories
What Is the Ansible find Module?
The ansible.builtin.find module searches for files matching specific criteria — name patterns, age, size, content, and type. It's the Ansible equivalent of the Linux find command, returning a list of matching files that you can feed into subsequent tasks for cleanup, backup, or processing.
Parameters Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
paths | list | required | Directories to search |
patterns | list | '*' | Filename patterns (shell glob or regex) |
use_regex | boolean | false | Treat patterns as Python regex |
age | string | — | File age filter (7d, 4w, -1h) |
age_stamp | string | mtime | Timestamp: atime, ctime, mtime |
size | string | — | Size filter (1m, -500k, 10g) |
file_type | string | file | Type: file, directory, link, any |
recurse | boolean | false | Search subdirectories |
hidden | boolean | false | Include hidden files |
contains | string | — | Regex to match file content |
excludes | list | — | Patterns to exclude |
depth | integer | — | Maximum recursion depth |
Find and Delete Old Logs
---
- name: Clean up old log files
hosts: all
become: true
tasks:
- name: Find logs older than 30 days
ansible.builtin.find:
paths: /var/log
patterns: '*.log,*.log.gz'
age: 30d
recurse: true
register: old_logs
- name: Show what will be deleted
ansible.builtin.debug:
msg: "Found {{ old_logs.matched }} files ({{ old_logs.examined }} examined)"
- name: Delete old logs
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ old_logs.files }}"
loop_control:
label: "{{ item.path }}"
Find Large Files
- name: Find files larger than 100MB
ansible.builtin.find:
paths: /home
size: 100m
recurse: true
register: large_files
- name: Report large files
ansible.builtin.debug:
msg: "{{ item.path }} — {{ (item.size / 1048576) | round(1) }}MB"
loop: "{{ large_files.files }}"
when: large_files.matched > 0
Find Files by Content
- name: Find configs with hardcoded passwords
ansible.builtin.find:
paths: /etc
patterns: '*.conf,*.ini,*.yml'
contains: 'password\s*[=:]\s*[^$]'
recurse: true
register: password_files
- name: Alert on found files
ansible.builtin.debug:
msg: "WARNING: {{ item.path }} may contain hardcoded passwords"
loop: "{{ password_files.files }}"
Find Empty Directories
- name: Find empty directories
ansible.builtin.find:
paths: /opt/app/data
file_type: directory
recurse: true
register: all_dirs
- name: Remove empty directories
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ all_dirs.files }}"
when: item.isdir and (item.path | basename) != 'data'
Find Recently Modified Files
- name: Find files modified in last hour
ansible.builtin.find:
paths: /etc
age: -1h
recurse: true
register: recent_changes
- name: Report recent changes (possible drift)
ansible.builtin.debug:
msg: "Changed: {{ item.path }} at {{ item.mtime }}"
loop: "{{ recent_changes.files }}"
Using Regex Patterns
- name: Find files matching regex
ansible.builtin.find:
paths: /var/backups
patterns: '^backup-\d{4}-\d{2}-\d{2}\.tar\.gz$'
use_regex: true
Return Values
The module returns a list of file objects with these properties:
| Property | Description |
|---|---|
path | Full file path |
size | Size in bytes |
mtime | Modification time (epoch) |
mode | File permissions |
uid | Owner UID |
gid | Group GID |
isdir | Is directory |
islnk | Is symlink |
matched | Total files matched |
examined | Total files examined |
Troubleshooting
- No files found — Check
recurse: truefor subdirectories - Age filter direction — Positive age (
30d) = older than; negative (-1h) = newer than - Size units — k=kilobytes, m=megabytes, g=gigabytes
- Permission denied — Use
become: truefor system directories - Too many results — Add
depthparameter to limit recursion
Related Articles
Conclusion
The ansible.builtin.find module replaces ad-hoc find commands with structured, idempotent file discovery. Combine it with ansible.builtin.file for cleanup, ansible.builtin.fetch for collection, or ansible.builtin.debug for auditing. The age, size, and content filters make it perfect for log rotation, disk cleanup, and security audits.