Ansible find Module with Practical Examples

Ansible Find Module

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 depending 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.

Basic Syntax

The basic syntax of the find module is simple. Here is a simple example:

- name: Find files in /tmp directory
  ansible.builtin.find:
    paths: /tmp
  register: found_files

Here are the key parameters :

  • 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: Search directories recursively (default: true).
  • hidden: Include hidden files (default: false).
  • follow: 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 using the debug module.

Example 2: Finding Files Based on Size

You can search for files according to 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

This playbook searches for files larger than 100MB in the /var/log directory.

Example 3: Finding Files Based on Modification Time

To find files as per 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

You can also combine 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 using the file module.

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:

- 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 depending 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 with a loop to display the paths of the deleted files for verification.

Conclusion

The find module in Ansible is a versatile tool for locating files and directories based on various criteria. By leveraging its features, you can automate complex file management tasks across multiple remote servers.

FAQs

1. What is the Ansible find module used for?

It is used to locate files and directories on remote hosts depending 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 this module to filter files depending 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.

About Hitesh Jethva

I am Hitesh Jethva, Founder and Author at Code2DevOps.com. With over 15 years of experience in DevOps and open source technologies, I am passionate about empowering teams through automation, continuous integration, and scalable solutions.

View all posts by Hitesh Jethva