Updated March 31, 2023
Introduction to Ansible find
Ansible find module or command as the name suggests is used to search the file, directory, or link recursively if specified and based on the single or multiple criteria like age, regular expression, or pattern, and this module is a part of the ansible-base which is included default in the ansible installation which generally works with the Unix system and to use this module with the Windows system we need to use the ansible windows module, ansible.windows.win_find.
Syntax of Ansible find
The syntax for this module:
“find:”
And they can be used with single or multiple parameters, as shown below. Not all parameters are covered, but only those which are required in the regular search operation.
1. paths: List of paths to include for the search. All paths should be fully qualified.
2. age:
- Selects the files equal or greater than the age specified in time.
- Negative age searches the files equal or less than the age specified in time.
- Age can be in seconds, minutes, hours, days, or weeks (e.g. 1w – 1 week, 2d – 2days).
3. recurse: Default No.
- If specified yes, then the directory searches the files recursively.
4. file_type: Searches the type based on the choice below:
- file (default)
- directory
- link
- any
5. excludes: Excludes the items based on the string or the specified pattern.
6. depth: Maximum level of the depth to search for. By default is unlimited, and if the specified recurse parameter value is no, then the depth is 1.
7. contains: Only works with the file_type=file to search the files based on their content.
8. pattern: Searches the items based on the regex pattern.
9. size:
- Selects the files based on the size equal to or greater than the specified size.
- If the size is negative, then find the files equal or less than the specified size.
- The default size is in bytes. However, you can explicitly mention (b=bytes, k=kilobytes, m=megabytes, g=gigabytes, t=terabytes).
10. hidden: The default value is no. If specified, yes, it includes hidden files.
11. read_whole_file: The default value is no. If specified yes, the whole file is read into memory instead of the line-by-line pattern, which can impact the server performance if the file size is large.
How does find Module Work in Ansible?
The find module ships with the ansible-base, which means it is included by default for the in ansible installation, and as described in the above syntax, we can use the various parameters to search files, directories, or links.
When we work with the windows search for the items, we need to use ansible.windows.win_find module, which uses almost the same parameters as specified above.
The following playbook will search the files from the Windows servers c:\temp folders.
Code:
---
- name: Ansible playbook to use the find module for Windows OS
hosts: winservers
tasks:
- name: Search the file from the C:\temp directories
ansible.windows.win_find:
paths: c:\temp
Output:
When you run the above playbook, you won’t get any output because the output is not stored anywhere. To store the retrieved output, we need to use the register variable as shown below.
Code:
tasks:
- name: Search the file from the C:\temp directories
ansible.windows.win_find:
paths: c:\temp
register: myfiles
- debug:
msg: "{{ myfiles }}"
Output:
To further dig down the output and retrieve only the file names, we can modify the JSON query below.
Code:
tasks:
- name: Search the file from the C:\temp directories
ansible.windows.win_find:
paths: c:\temp
register: myfiles
- debug:
msg: "{{ myfiles | json_query('files[*].filename') }}"
Now you can see the file names in the output.
Output:
Examples of Ansible find
Given below are the examples of Ansible find:
Example #1
Recursively search the /etc. folder files older than 1 week and greater than 1kb.
Code:
---
- name: Ansible playbook to use the find files on the Unix servers
hosts: linuxservers
tasks:
- name: Search the file from the /tmp directories
find:
paths: /etc
recurse: yes
age: 1w
size: 1k
register: myfiles
- debug:
msg: "{{ myfiles | json_query('files[*].path') }}"
Output:
Example #2
Search the *.ps1, *.txt files.
The below playbook will search the files on the multiple folders with the specific extensions (ps1 and txt) and whose size is less than 1mb, store the output in the variable, and manipulate the output by the JSON query.
Code:
---
- name: Ansible playbook to use the find module for Windows OS
hosts: winservers
tasks:
- name: Search the file from the directories
ansible.windows.win_find:
paths:
- c:\temp
- c:\scripts
size: -1m
patterns: '*.ps1,*.txt'
register: myfiles
- debug:
msg: "{{ myfiles | json_query('files[*].filename')}}"
An alternate way, you can use the regex pattern to search the files as shown below.
Code:
tasks:
- name: Search the file from the C:\temp directories
ansible.windows.win_find:
paths:
- c:\temp
- c:\scripts
size: -1m
patterns: "^.*?\\.(?:ps1|txt)$"
use_regex: yes
Output:
Example #3
Search all the directories from the root folder, including the hidden directories.
Code:
---
- name: Ansible playbook to use the find folders on the Unix servers
hosts: linuxservers
tasks:
- name: Search the directories from the root folder
find:
paths: /
file_type: directory
hidden: yes
register: myfiles
- debug:
msg: "{{ myfiles | json_query('files[*].path') }}"
The above playbook searches the root folder directories, including hidden directories.
Example #4
Exclude folders.
The below playbook will search all the directories from the /etc. folders and will exclude 3 directories (python3, selinux, and lvm).
Code:
---
- name: Ansible playbook to use the find files on the Unix servers
hosts: linuxservers
tasks:
- name: Search the directories
find:
paths: /etc
file_type: directory
excludes: 'python3,selinux,lvm'
register: myfiles
- debug:
msg: "{{ myfiles | json_query('files[*].path') }}"
Conclusion
Ansible builtin module find and the windows module win_find work like the shell, batch, or PowerShell language that we use for searching the files or directories, but we don’t need to manipulate commands in ansible to get the desired search result; but instead, we can use the parameters that directly help us to search the files and the folders like file age, size, etc.
Recommended Articles
This is a guide to Ansible find. Here we discuss the introduction; how does find module work in ansible? And examples, respectively. You may also have a look at the following articles to learn more –