It can also be said that the find module is very similar to using the “find” command in Linux. Of course, the parameters and operators for both the module and command are different, but they work in the same way.
If you want to learn how to find files and folders using Ansible, you have come to the right place as we will be giving you a comprehensive guide on the find module.
Parameters of Find Module
Find can search for your files based on different attributes. You can apply filters such as the age of the file, the last accessed date, modified date, etc. These filters can be specified in the parameters while using the find module. The different parameters available are:
Age: as mentioned before, the age parameter can be specified with find so that Ansible can search for files that are “xyz” days old or are of “xyz” age.
Contains: in case you remember what was written in the file you are looking for, you can specify that string or regex pattern with the “contains” parameter. The system will check the files for that particular string and give you the files that contain that string.
Depth: this parameter is used to specify the number of levels of directories the system should go to check for the particular file you are looking for.
File_type: with this parameter, you specify the “file type” the system is supposed to search for. Using “any,” you tell the system to check for every file type present in the memory. You can also choose to look for the file in a specific directory. Another option is to specify the file type. The module will then look at only the specified file type.
Hidden: some files become hidden. With this parameter, you can tell the module to check for the file you are looking for among the hidden files as well.
Paths: as the name suggests, this parameter specifies the directory paths that the system will search to find the file you want to access.
Patterns: these patterns are the shell and regex patterns. Using this parameter, we can specify the module to look for files having these regex or shell patterns in their basenames. When regex patterns are specified, the module looks for the file having similar regex patterns in their base names and returns those files to you as output. This parameter also expects a list of different regex patterns to be given.
Excludes: this parameter is combined with the “patterns” parameter. It excludes telling the module to look for files not having the regex pattern specified.
Read_whole_file: this parameter is combined with the “contains” parameter. It instructs the system to look for the regex pattern specified with the “contains” in the whole file.
Recurse: the recurse pattern specifies that the module looks for the file by moving recursively (upwards) between the directories.
Size: this parameter specifies the file size. When we pass a file size to the module, it looks for files having a size larger than the specified one. To look for files having sizes smaller than the one specified, use the minus(-) sign before the numerical value.
Use_regex: this parameter has a boolean value. If it is “true,” the module searches the files for the regex pattern specified. In case it is “false,” the module looks for files having shell patterns in their basenames.
These were the parameters available with the find module. Next, we look at the return values.
Examined: this tells us the number of files the module has examined while looking for the object specified.
Files: these are all the files that have matched the query we gave to the module.
Matched: the number of files that have matched our query.
Skipped_paths: this tells us the paths skipped while finding the object and why they were skipped.
Examples
find:
paths: /xyz
age: 2d
recurse: yes
Once that’s done, you’d have to run the following command on the Linux shell:
In the above example, we have specified the paths of files and the age of those files. The module will look in the specified directory and give us those files whose age is greater than 10 days.
find:
paths: /var/log
recurse: no
file_type: directory
excludes: 'xyz'
To run this playbook, the following command would have to be executed on the Command Terminal:
Here, we have specified the file type and the paths to look for. The file_type has been specified as “directory,” so the module will search all the directories. The “excludes” parameter we have used is to specify an arbitrary regex pattern not to look for.
Conclusion
In this article, we looked at the Ansible find a module. Find can be used to look for a specific file in our system. We also looked at the different parameters available with find. The different parameters available really help us narrow down the search; this makes the module more efficient.
If you were hoping to find a lost file using Ansible, we hope that we helped you understand how you could do that using the find module. For further queries, please let us know in the comments.