The find module in Ansible helps automate the process of locating files and directories across multiple remote servers. This is useful for cleaning up old files, gathering information, or performing operations on a subset of files. It allows users to locate files based on various criteria, such as size, modification time, permissions, and more.
This guide will walk you through the usage of the find module with practical examples to help you automate file management tasks efficiently.
Table of Contents
Basic Syntax of the Find Module
The basic syntax of the find module is straightforward. Here is a simple example that demonstrates how to use the find module:
- name: Find files in /tmp directory
ansible.builtin.find:
paths: /tmp
register: found_files
Here are the parameters available in the find module:
- paths: List of directories to search in.
- patterns: List of shell-style wildcard patterns to match file names.
- file_type: Type of file to look for (file, directory, link).
- size: Size of the file (e.g., +100m for files larger than 100MB, -50k for files smaller than 50KB).
- age: Age of the file (e.g., +7d for files older than 7 days, -1h for files newer than 1 hour).
- age_stamp: Time attribute to compare (mtime, atime, time).
- recurse: Whether to search directories recursively (default: true).
- hidden: Whether to include hidden files (default: false).
- follow: Whether to follow symbolic links (default: false).
- contains: List of strings the file content should contain.
- checksum_algorithm: Algorithm to use for file checksums (md5, sha1, sha256, etc.).
Example 1: Finding Files Based on Name
The patterns parameter allows you to find files based on their names. It supports shell-style wildcards.
Here is an example playbook.
---
- name: Find log files based on their name
hosts: all
tasks:
- name: Find all log files in /var/log
ansible.builtin.find:
paths: /var/log
patterns: '*.log'
register: log_files
- name: Display found log files
ansible.builtin.debug:
var: log_files.files
This playbook will search for all files with a .log extension in the /var/log directory and display the results.
Example 2: Finding Files Based on Size
The find module allows you to search for files based on their size using the size parameter.
Example playbook:
---
- name: Find files larger than 100MB
hosts: all
tasks:
- name: Find files larger than 100MB in /var/log
ansible.builtin.find:
paths: /var/log
size: +100m
register: large_files
- name: Display large files
ansible.builtin.debug:
var: large_files.files
In this example, the playbook searches for files larger than 100MB in the /var/log directory.
Example 3: Finding Files Based on Modification Time
To find files based on their modification time, you can use the age and age_stamp parameters. The age_stamp can be set to mtime (modification time), atime (access time), or ctime (change time).
Example playbook:
---
- name: Find files modified in the last 7 days
hosts: all
tasks:
- name: Find files modified in the last 7 days
ansible.builtin.find:
paths: /home/user
age: -7d
age_stamp: mtime
register: recent_files
- name: Display recently modified files
ansible.builtin.debug:
var: recent_files.files
This playbook finds files in the /home/user directory that were modified in the last 7 days.
Example 4: Finding Files with Specific Permissions
You can search for files with specific permissions using the file_type and mode parameters.
Example playbook:
---
- name: Find executable files
hosts: all
tasks:
- name: Find executable files in /usr/local/bin
ansible.builtin.find:
paths: /usr/local/bin
file_type: file
mode: '0755'
register: executable_files
- name: Display executable files
ansible.builtin.debug:
var: executable_files.files
In this example, the playbook finds all executable files with 0755 permissions in the /usr/local/bin directory.
Example 5: Combining Multiple Criteria
The find module supports combining multiple criteria to narrow down the search results.
Example playbook:
---
- name: Find old log files larger than 50MB
hosts: all
tasks:
- name: Find old log files larger than 50MB
ansible.builtin.find:
paths: /var/log
patterns: '*.log'
size: +50m
age: +30d
age_stamp: mtime
register: old_large_log_files
- name: Display old large log files
ansible.builtin.debug:
var: old_large_log_files.files
This playbook searches for log files in the /var/log directory that are larger than 50MB and older than 30 days.
Example 6: Using Find Results in Other Tasks
The results from the find module can be used in other tasks to perform additional operations, such as deleting old files.
---
- name: Find and delete old log files
hosts: all
tasks:
- name: Find log files older than 90 days
ansible.builtin.find:
paths: /var/log
patterns: '*.log'
age: +90d
age_stamp: mtime
register: old_log_files
- name: Delete old log files
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ old_log_files.files }}"
In this example, the playbook finds log files older than 90 days and deletes them.
Real-World Use Case: Cleaning Up Old Backup Files
Let’s consider a scenario where you manage a server that generates daily backup files stored in the /backup directory. Over time, these backup files can accumulate and consume significant disk space. You need to implement a solution to automatically clean up backup files older than 30 days to free up disk space.
Here’s how you can achieve this using the Ansible find module:
- name: Cleanup old backup files
hosts: backup_server
tasks:
- name: Find backup files older than 30 days
ansible.builtin.find:
paths: /backup
patterns: '*.tar.gz'
age: +30d
age_stamp: mtime
register: old_backup_files
- name: Delete old backup files
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ old_backup_files.files }}"
- name: Display deleted backup files
debug:
msg: "Deleted {{ item.path }}"
loop: "{{ old_backup_files.files }}"
The above playbook performs the following tasks.
- Finding Old Backup Files: The first task search for backup files (*.tar.gz) in the /backup directory that are older than 30 days based on their modification time (time).
- Deleting Old Backup Files: The second task uses a file module to delete each file found in the previous step by looping through the old_backup_files.files list.
- Displaying Deleted Files: The third task uses a debug module to display the paths of the deleted files for verification.
Conclusion
The Ansible find module is a versatile tool for locating files and directories based on various criteria. By leveraging the power of this module, you can automate complex file management tasks across multiple remote servers.
FAQs
1. What is the Ansible find module used for?
The Ansible find module is used to locate files and directories on remote hosts based on specific criteria, such as file name, size, age, or type.
2. How do I use the find module to locate all files in a directory?
Use the paths parameter to specify the directory, and set recurse to yes to search all files within that directory.
3. How do I find files modified in the last 24 hours?
Use the age parameter with the find module to filter files based on modification time.
4. How do I find files that belong to a specific user?
Use the file_type and uid parameters to locate files owned by a specific user.
5. How do I print the list of found files?
Use the debug module to print the paths of the found files.